2013-03-12 160 views
-2

我想分析以下數據,這是來自服務器的對象列表。這是我有我的數據使用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。

+0

解析它會對其進行字符串化。你爲什麼首先把它串起來? – Quentin 2013-03-12 13:22:16

+0

以前,我在做$ .parseJSON(data.d),它給了undefined .. – faizanjehangir 2013-03-12 13:23:28

+3

你爲什麼要解析已經是JavaScript對象的東西?只需使用'data.d'。 – Quentin 2013-03-12 13:24:01

回答

2

剛開始使用data.d [I] .empProperty並且如在一些評論提到data.d [I] .empValue i是陣列的索引。不要將它串起來,它已經被解析成了一個對象。

閱讀關於JSON Here

-1

該結構是一個對象數組。 所以,你可以簡單地通過索引來訪問每個元素:

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

然後你可以用不同的方式來訪問每個屬性:

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

here

+1

請添加評論,除了你的downvote – 2013-03-12 13:29:21

+3

我沒有downvote,但我認爲'var arr = JSON.stringify(data.d);'將在arr中設置data.d的字符串表示,所以'arr [0 ]'只會返回該字符串的第一個字符。 – Jacopofar 2013-03-12 13:34:36

+0

@Jackopo:請看看演示。它工作正常。 – 2013-03-12 13:50:55

-1

我引述以下鏈接部分:JSON

爲了抵禦這,應使用JSON解析器。 JSON解析器將只識別JSON文本,拒絕所有腳本。

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

正如Quentin在上面的評論中提到的那樣,爲什麼我會解析一個對象,它已經是一個JS對象,轉換成JSON ... – faizanjehangir 2013-03-12 13:31:35

+0

你對對象進行了stringify,把它放到一個對象的方式是:解析 – 2013-03-12 13:35:40

+0

但是'data .d'是**輸入**來stringify,而不是輸出! – Quentin 2013-03-12 13:40:11