Start a new topic

Unable to parse json array (android)

Hi, I am really giving up hope, but I need to parse Json array from my collection such as:


[

  {

    "username": "test",

    "rating": 5

  },

  {

    "username": "test2",

    "rating": 3

  }

]


Any ideas how can I do that?


P.S. I haven't seen such not working examples as yours... sorry


Hi Daniel,

Looking at the code snippets, and the error "Property: 'Numbers' is invalid or does not exist." it looks to me that the property "Numbers" is not an array as it is expected. You may create an empty array instead of just defining the variable to make sure the value is an array:

// create an empty array for the property value as the property value is being assigned to the testModel.Numbers
let value = [];

Also, when you are adding objects to an observable array you should use the methods described here as otherwise you would be overwriting the observable array object with plain JavaScript object which would not notifies any subscribers (in this case the listView) when a change occurs.

This problem seems to be more related to NativeScript than Kinvey backend, so please note that for any NativeScript related issues it is best to seek NativeScript expertise on the community slack channel (nativescriptcommunity.slack.com) or in the community forum at discourse.nativescript.org.

I hope this has helped.

Regards

Martin


this is how the object returned looks like, but am trying to see how to bind this to the view using the view-model. thanks alot

{

JS: "_id": "1",

JS: "foodName": "Rice",

JS: "foodDescript": "with stew and chicken",

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: },

JS: "_kmd": {

JS: "lmt": "2017-12-28T23:08:19.311Z",

JS: "ect": "2017-12-27T16:28:17.893Z"

JS: }

JS: }

JS: === dump(): dumping function and properties names ===

JS: === dump(): finished ===

JS: === dump(): dumping members ===

JS: {

JS: "_id": "5a457c87b5b2fe6982663ac2",

JS: "foodName": "Beans",

JS: "foodDescript": "with vegetables",

JS: "_kmd": {

JS: "lmt": "2017-12-28T23:21:43.038Z",

JS: "ect": "2017-12-28T23:21:43.038Z"

JS: },

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: }

JS: }

JS: === dump(): dumping function and properties names ===

JS: === dump(): finished ===

JS: === dump(): dumping members ===

JS: {

JS: "_id": "5a457cabebee48595acd6a13",

JS: "foodName": "Porrage yam",

JS: "foodDescript": "Its tick and also very sweet",

JS: "_kmd": {

JS: "lmt": "2017-12-28T23:22:19.209Z",

JS: "ect": "2017-12-28T23:22:19.209Z"

JS: },

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: }

JS: }

JS: === dump(): dumping function and properties names ===

JS: === dump(): finished ===

JS: === dump(): dumping members ===

JS: {

JS: "_id": "5a47576d87b4b32431491c55",

JS: "foodName": "cake and akara",

JS: "foodDescript": "prepared with pap and custard",

JS: "_kmd": {

JS: "lmt": "2017-12-30T09:07:57.861Z",

JS: "ect": "2017-12-30T09:07:57.861Z"

JS: },

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: }

JS: }

JS: === dump(): dumping function and properties names ===

JS: === dump(): finished ===

JS: === dump(): dumping members ===

JS: {

JS: "_id": "1",

JS: "foodName": "Rice",

JS: "foodDescript": "with stew and chicken",

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: },

JS: "_kmd": {

JS: "lmt": "2017-12-28T23:08:19.311Z",

JS: "ect": "2017-12-27T16:28:17.893Z"

JS: }

JS: }

JS: === dump(): dumping function and properties names ===

JS: === dump(): finished ===

JS: === dump(): dumping members ===

JS: {

JS: "_id": "5a457c87b5b2fe6982663ac2",

JS: "foodName": "Beans",

JS: "foodDescript": "with vegetables",

JS: "_kmd": {

JS: "lmt": "2017-12-28T23:21:43.038Z",

JS: "ect": "2017-12-28T23:21:43.038Z"

JS: },

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: }

JS: }

JS: === dump(): dumping function and properties names ===

JS: === dump(): finished ===

JS: === dump(): dumping members ===

JS: {

JS: "_id": "5a457cabebee48595acd6a13",

JS: "foodName": "Porrage yam",

JS: "foodDescript": "Its tick and also very sweet",

JS: "_kmd": {

JS: "lmt": "2017-12-28T23:22:19.209Z",

JS: "ect": "2017-12-28T23:22:19.209Z"

JS: },

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: }

JS: }

JS: === dump(): dumping function and properties names ===

JS: === dump(): finished ===

JS: === dump(): dumping members ===

JS: {

JS: "_id": "5a47576d87b4b32431491c55",

JS: "foodName": "cake and akara",

JS: "foodDescript": "prepared with pap and custard",

JS: "_kmd": {

JS: "lmt": "2017-12-30T09:07:57.861Z",

JS: "ect": "2017-12-30T09:07:57.861Z"

JS: },

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: }

JS: }

JS: === dump(): dumping function and properties names ===

JS: === dump(): finished ===

Executing before-prepare hook from C:\Users\DANIEL\nativescript_apps\what\Cafe\hooks\before-prepare\nativescript-dev-sass.js

Preparing project...

Project successfully prepared (Android)

Executing after-prepare hook from C:\Users\DANIEL\nativescript_apps\what\Cafe\hooks\after-prepare\nativescript-dev-sass.js

Successfully transferred breakfast.xml.

Refreshing application...

JS: Binding: Property: 'foodName' is invalid or does not exist. SourceProperty: 'foodName'

Successfully synced application org.nativescript.Cafe on device 99IBZPYT99999999.

JS: === dump(): dumping members ===

JS: {

JS: "_id": "1",

JS: "foodName": "Rice",

JS: "foodDescript": "with stew and chicken",

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: },

JS: "_kmd": {

JS: "lmt": "2017-12-28T23:08:19.311Z",

JS: "ect": "2017-12-27T16:28:17.893Z"

JS: }

JS: }

JS: === dump(): dumping function and properties names ===

JS: === dump(): finished ===

JS: === dump(): dumping members ===

JS: {

JS: "_id": "5a457c87b5b2fe6982663ac2",

JS: "foodName": "Beans",

JS: "foodDescript": "with vegetables",

JS: "_kmd": {

JS: "lmt": "2017-12-28T23:21:43.038Z",

JS: "ect": "2017-12-28T23:21:43.038Z"

JS: },

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: }

JS: }

JS: === dump(): dumping function and properties names ===

JS: === dump(): finished ===

JS: === dump(): dumping members ===

JS: {

JS: "_id": "5a457cabebee48595acd6a13",

JS: "foodName": "Porrage yam",

JS: "foodDescript": "Its tick and also very sweet",

JS: "_kmd": {

JS: "lmt": "2017-12-28T23:22:19.209Z",

JS: "ect": "2017-12-28T23:22:19.209Z"

JS: },

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: }

JS: }

JS: === dump(): dumping function and properties names ===

JS: === dump(): finished ===

JS: === dump(): dumping members ===

JS: {

JS: "_id": "5a47576d87b4b32431491c55",

JS: "foodName": "cake and akara",

JS: "foodDescript": "prepared with pap and custard",

JS: "_kmd": {

JS: "lmt": "2017-12-30T09:07:57.861Z",

JS: "ect": "2017-12-30T09:07:57.861Z"

JS: },

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: }

JS: }

JS: === dump(): dumping function and properties names ===

JS: === dump(): finished ===

JS: === dump(): dumping members ===

JS: {

JS: "_id": "1",

JS: "foodName": "Rice",

JS: "foodDescript": "with stew and chicken",

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: },

JS: "_kmd": {

JS: "lmt": "2017-12-28T23:08:19.311Z",

JS: "ect": "2017-12-27T16:28:17.893Z"

JS: }

JS: }

JS: === dump(): dumping function and properties names ===

JS: === dump(): finished ===

JS: === dump(): dumping members ===

JS: {

JS: "_id": "5a457c87b5b2fe6982663ac2",

JS: "foodName": "Beans",

JS: "foodDescript": "with vegetables",

JS: "_kmd": {

JS: "lmt": "2017-12-28T23:21:43.038Z",

JS: "ect": "2017-12-28T23:21:43.038Z"

JS: },

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: }

JS: }

JS: === dump(): dumping function and properties names ===

JS: === dump(): finished ===

JS: === dump(): dumping members ===

JS: {

JS: "_id": "5a457cabebee48595acd6a13",

JS: "foodName": "Porrage yam",

JS: "foodDescript": "Its tick and also very sweet",

JS: "_kmd": {

JS: "lmt": "2017-12-28T23:22:19.209Z",

JS: "ect": "2017-12-28T23:22:19.209Z"

JS: },

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: }

JS: }

JS: === dump(): dumping function and properties names ===

JS: === dump(): finished ===

JS: === dump(): dumping members ===

JS: {

JS: "_id": "5a47576d87b4b32431491c55",

JS: "foodName": "cake and akara",

JS: "foodDescript": "prepared with pap and custard",

JS: "_kmd": {

JS: "lmt": "2017-12-30T09:07:57.861Z",

JS: "ect": "2017-12-30T09:07:57.861Z"

JS: },

JS: "_acl": {

JS: "creator": "5a42e63543691b7bcb4b7e50"

JS: }

JS: }

Thanks a lot, i am really grateful.....I cant even express how much I am grateful and feel like someone's there for me. thanks.

I have another issue pls, i am trying to bind the retrieved data to my xml view but my view model is giving me headaches, it keeps on giving me this log: JS: Binding: Property: 'Numbers' is invalid or does not exist. SourceProperty: 'Numbers'

, could you pls look at the files for me? I would be very grateful....here are the files:

image

image

image



Hi Daniel,

Based on the provided code sample, the received "entities" is of type object, so you should not JSON.parse() it. The code would look like this:

            const subscription = dataStore.find()
              .subscribe((entities) => {
                   // entities is of type object, so you should not JSON.parse it
                   for (item of entities) {
                       console.log(item);
                  }
              }, (error) => {
                   console.log(error);
              }, () => {
                   console.log('done');
              })

Let me know if this has fixed the issue.

Regards,

Martin

Yes, the parsing problem is in the app itself (cafe) ant the version of kinvey-nativescript-sdk i am using is version: 3.9.4.

a snippet of my package.json is shown below.

Thanks alot and sorry for the late reply, #

image

new stuff.

Hello Daniel,


For the JSON returned by Kinvey, are you having the parsing problem when querying the backend using the Kinvey Console or in the "cafe" app itself?


If the Kinvey Console, can you send the query that you are using to retrieve the data and the snippet of code that is performing the JSON parsing?


If it is failing in the "cafe" app, what version of the SDK are you using. I can see that your using a Javascript SDK but I am unable to determine the exact version number that you are using in our logs.


Regards,


Billy Gee

I have gone through the kinvey documentation a 101 times, in fact i'm using it step by step - its always open in my browser.

but it has not helped me for this issue, there is no reference there for this issue.

i was not sure the first code snippet uploaded well. Thanks alotimage


i am facing the same problem too, i can't parse json object returned to me from kinvey. Here are my smaples:

image

image

Giedrius,

Can you send me the sample project where you are trying to parse Json array from Kinvey collection and facing issues?

Thanks,
Pranav
Kinvey

 

I have found how the queries works, but json array question is still valid

By the way, how can I make a query of two fields?

I have been looking at those examples for ages, they are not helping me for this question...


Whatever, I found a workaround

Giedrius,

Please take a look at this link:

http://devcenter.kinvey.com/android/guides/datastore#Fetching

 

Thanks,

Pranav

Kinvey

Login or Signup to post a comment