user659469
user659469

Reputation: 325

Extract Json data on client side

I have following JSON data coming to client. I need to extract the data somehow so I can loop through it to get all name & count values.

{
 "summarydata": {
    "rows": [
      {
        "name": "Cisco0 Webinar US",
        "count": "1"
      },
      {
        "name": "Resource Nation CC",
        "count": "1"
      },
      {
        "name": "test",
        "count": "10"
      },
      {
        "name": "test",
        "count": "2"
      },
      {
        "name": "Vendor Seek",
        "count": "1"
      }
    ]
  }
}

$.extend($.jgrid.defaults,   
      { datatype: 'jsonstring' },
      { ajaxGridOptions: { contentType: "application/json",   
      success: function (data, textStatus) { 

          if (textStatus == "success") {   
              var thegrid = $("#BuyBackGrid")[0];
              thegrid.addJSONData(data.data);  
              var summaryresult = $.parseJSON(data.summarydata.rows[0]);

              alert(summaryresult );// this gives me null
              alert(data.summarydata.rows[0].name); //this gives me first name element which is "Cisco0 Webinar US" in my case.

             // alert($.parseJSON(data).summarydata.rows[0].name);               

                            }   
      } //end of success  
      }//end of ajaxGridOptions   
    });

Upvotes: 4

Views: 5604

Answers (1)

mattsven
mattsven

Reputation: 23293

Leveraging jQuery...

The $.getJSON() function parses a local JSON file and returns it as an object.

$.getJSON("myJSON.js", function(json){
    alert(json.summarydata.rows[0].name);
});

You could also just do this, using the JSON library for javascript (the object is also standard in most recent browsers).

alert(JSON.parse(myJSONString).summarydata.rows[0].name);

Upvotes: 6

Related Questions