Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse complex JSON in Sencha Touch

I've asked over on the Sencha Touch forums but haven't had a great response (well, none in fact). I wonder if anyone can help. I want to create a List of items from a JSON response received from a PHP script. Here's an example of the JSON received back from the PHP:

{
   "friends":[
      {
         "friend":{
            "id":"4",
            "forename":"Chris",
            "surname":"Major",
            "verboseName":"Chris Major",
            "phoneNumber":"07931655247",
            "longitude":"-0.410909",
            "latitude":"52.999245",
            "email":"[email protected]",
            "lastRefresh":null,
            "joinDate":"1294839423"
         },
         "networks":null,
         "approved":"1"
      },
      {
         "friend":{
            "id":"2",
            "forename":"Marta",
            "surname":"Urbanowicz",
            "verboseName":"Marta Urbanowicz",
            "phoneNumber":"07716021468",
            "longitude":"-0.0338518",
            "latitude":"52.9773876",
            "email":"[email protected]",
            "lastRefresh":null,
            "joinDate":"1294836801"
         },
         "networks":null,
         "approved":"1"
      }
   ]
}

I would like to know how I can parse this into a DataStore and then output a grouped List in Sencha Touch. Although there are examples of using standard JSON responses for a ListView, there doesn't seem to be a nested example such as this...

Any help gratefully received.

like image 606
BenM Avatar asked May 18 '26 00:05

BenM


1 Answers

Ben,

What you may want to do is declare your store of Friends with the fields with the fields you desire (maybe id, surname, email, network, approved)

Then when make a remote call with the Ajax remote call, which will just pass back the raw data.

At that point you'll have the "friends" object. You can then loop through each "friend" and instead of adding the "friend" in total create new objects:

var friend = {id:'2', surname:'whatever', email:'[email protected]'}

Make an array of those new friend structures, and pass those into the store. This will flatten out or simplify your complex json structure.

Hope that helps, and I can provide more details if you need them.

like image 194
ballmw Avatar answered May 19 '26 15:05

ballmw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!