-2

我想解析以下数据,这是来自服务器的对象列表。这是我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、comments 作为其属性组成。我无法在 JS 中访问这些属性。

4

3 回答 3

2

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

在此处阅读有关 JSON 的信息

于 2013-03-12T13:48:34.623 回答
-1

该结构是一个对象数组。因此,您可以简单地通过索引访问每个元素:

var arr = JSON.stringify(data.d);
var item = arr[0];

然后您可以通过不同的方式访问每个属性:

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

这里的例子。

于 2013-03-12T13:25:26.107 回答
-1

我引用以下链接中的一段:JSON

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

var jsonData = JSON.stringify(data.d);
var myObject = JSON.parse(jsonData);
于 2013-03-12T13:29:24.133 回答