C# Javascript:解析JSON对象

C# Javascript:解析JSON对象,c#,javascript,jquery,json,C#,Javascript,Jquery,Json,我想解析以下数据,这是来自服务器的对象列表。这是我在使用JSON.stringify(data.d)之后得到的在数据上: "[{"__type":"EditGridDemo.CellData","empProperty":"SSN","empValue":"a","isValid":false,"comments":"Reason of what went wrong"},{"__type":"EditGridDemo.CellData","empProperty":"Birth_Date","

我想解析以下数据,这是来自服务器的对象列表。这是我在使用
JSON.stringify(data.d)之后得到的在数据上:

"[{"__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和作为属性的注释。我无法在JS中访问这些属性

该结构是一个对象数组。 因此,您只需通过索引即可访问每个元素:

var arr = JSON.stringify(data.d);
var item = arr[0];
然后,您可以通过不同的方式访问每个属性:

empValue = item.empValue;  //returns "a"
empProperty = item["empProperty"];  //returns "SSN"

示例。

我引用了以下链接中的一节:

为了防止这种情况,应该使用JSON解析器。JSON解析器将只识别JSON文本,拒绝所有脚本

var jsonData = JSON.stringify(data.d);
var myObject = JSON.parse(jsonData);

只要开始使用data.d[i].empProperty和data.d[i].empValue,正如一些注释中提到的,i是数组的索引。不要字符串化它,它已经为您解析为一个对象


阅读关于JSON的文章

解析它与字符串化相反。为什么首先要对它进行字符串化?之前,我在做$.parseJSON(data.d),它给出了未定义的..为什么要尝试解析已经是JavaScript对象的东西?只需使用
data.d
。请在您的downvoteI之外添加一条注释,我没有进行downvote,但我认为
var arr=JSON.stringify(data.d)
将在arr中设置数据.d的字符串表示形式,因此
arr[0]
将只返回该字符串的第一个字符。@Jackopo:请查看演示。它工作得很好。演示没有使用
var arr=JSON.stringify(data.d)
,而是直接使用数据。正如昆汀在上面的评论中提到的,为什么我要将一个已经是JS对象的对象解析为JSON…你在对象stringify上做了什么,将其放入对象的方式是:parseBut
data.d
是stringify的输入,不是输出@昆汀,我错了,我编辑了答案