在客户端提取Json数据
问题描述:
我有以下JSON数据传到客户端。我需要以某种方式提取数据,所以我可以通过循环来获取所有名称的计数值&。在客户端提取Json数据
{
"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
});
答
利用jQuery ...
的$.getJSON()
函数解析本地JSON文件,并将它作为一个对象。
$.getJSON("myJSON.js", function(json){
alert(json.summarydata.rows[0].name);
});
你也可以只做到这一点,使用JSON library对JavaScript(对象也是最新的浏览器的标准)。
alert(JSON.parse(myJSONString).summarydata.rows[0].name);
什么语言? –
你使用任何库 - 在JSON上? – bensiu
我使用asp.net处理程序进行Ajax调用并将JSON数据返回给客户端。 response.Add(“summarydata”,summarytoken); – user659469