Javascript:解析JSON对象
我想分析以下数据,这是来自服务器的对象列表。这是我有我的数据使用JSON.stringify(data.d);
后:Javascript:解析JSON对象
"[{"__type":"EditGridDemo.CellData","empProperty":"SSN","empValue":"a","isValid":false,"comments":"Reason of what went wrong"},{"__type":"EditGridDemo.CellData","empProperty":"Birth_Date","empValue":"","isValid":false,"comments":"Reason of what went wrong"},{"__type":"EditGridDemo.CellData","empProperty":"Department_Name","empValue":"","isValid":false,"comments":"Reason of what went wrong"},{"__type":"EditGridDemo.CellData","empProperty":"email","empValue":"","isValid":false,"comments":"Reason of what went wrong"},{"__type":"EditGridDemo.CellData","empProperty":"First_Name","empValue":"","isValid":false,"comments":"Reason of what went wrong"},{"__type":"EditGridDemo.CellData","empProperty":"Sex","empValue":"","isValid":false,"comments":"Reason of what went wrong"},{"__type":"EditGridDemo.CellData","empProperty":"Strata_ID","empValue":null,"isValid":false,"comments":"Reason of what went wrong"},{"__type":"EditGridDemo.CellData","empProperty":"SSN","empValue":"b","isValid":false,"comments":"Reason of what went wrong"},{"__type":"EditGridDemo.CellData","empProperty":"Birth_Date","empValue":"","isValid":false,"comments":"Reason of what went wrong"},{"__type":"EditGridDemo.CellData","empProperty":"Department_Name","empValue":"","isValid":false,"comments":"Reason of what went wrong"},{"__type":"EditGridDemo.CellData","empProperty":"email","empValue":"","isValid":false,"comments":"Reason of what went wrong"},{"__type":"EditGridDemo.CellData","empProperty":"First_Name","empValue":"","isValid":false,"comments":"Reason of what went wrong"},{"__type":"EditGridDemo.CellData","empProperty":"Sex","empValue":"","isValid":false,"comments":"Reason of what went wrong"},
{"__type":"EditGridDemo.CellData","empProperty":"Strata_ID","empValue":null,"isValid":false,"comments":"Reason of what went wrong"}]"
这是CellData,其中包括列表empProperty,empValue,isValid方法,评价其attributes..I不能够访问这些属性JS。
刚开始使用data.d [I] .empProperty并且如在一些评论提到data.d [I] .empValue i是阵列的索引。不要将它串起来,它已经被解析成了一个对象。
阅读关于JSON Here
该结构是一个对象数组。 所以,你可以简单地通过索引来访问每个元素:
var arr = JSON.stringify(data.d);
var item = arr[0];
然后你可以用不同的方式来访问每个属性:
empValue = item.empValue; //returns "a"
empProperty = item["empProperty"]; //returns "SSN"
例here。
请添加评论,除了你的downvote – 2013-03-12 13:29:21
我没有downvote,但我认为'var arr = JSON.stringify(data.d);'将在arr中设置data.d的字符串表示,所以'arr [0 ]'只会返回该字符串的第一个字符。 – Jacopofar 2013-03-12 13:34:36
@Jackopo:请看看演示。它工作正常。 – 2013-03-12 13:50:55
我引述以下链接部分:JSON
为了抵御这,应使用JSON解析器。 JSON解析器将只识别JSON文本,拒绝所有脚本。
var jsonData = JSON.stringify(data.d);
var myObject = JSON.parse(jsonData);
正如Quentin在上面的评论中提到的那样,为什么我会解析一个对象,它已经是一个JS对象,转换成JSON ... – faizanjehangir 2013-03-12 13:31:35
你对对象进行了stringify,把它放到一个对象的方式是:解析 – 2013-03-12 13:35:40
但是'data .d'是**输入**来stringify,而不是输出! – Quentin 2013-03-12 13:40:11
解析它会对其进行字符串化。你为什么首先把它串起来? – Quentin 2013-03-12 13:22:16
以前,我在做$ .parseJSON(data.d),它给了undefined .. – faizanjehangir 2013-03-12 13:23:28
你为什么要解析已经是JavaScript对象的东西?只需使用'data.d'。 – Quentin 2013-03-12 13:24:01