2014-02-14 113 views
0

我想獲得這個json數組。我怎樣才能json數據jquery

data='{"Status":200,"Method":"SelectCompany","Errors":[],"Data":[{"Name":"MoreTimeAPITest","OrgNumber":"1234561234","APIKey":"168080BF-77A9-4B64-94C3-A5B3B4512A1C","Login":"moretime","Password":"SvMrqwk4G3"}],"DataType":{"Type":"list","ObjectType":"APIConnections"},"ResponseType":"json","API_Version":"1","API_Name":"PromikBook API","Server_Time":"2014-02-13 13:39:56.62275"}'; 

var newArr = []; 

我用這段代碼。但它不起作用。

$.each(data, function(i, obj) { 
    console.log([obj.Status]); 

    newArr.push([obj.Status]); 
}); 
+1

數據是一個字符串不是一個對象 –

+0

@user ......你需要解析JSON –

+0

目前尚不清楚你想要的結果是什麼。您在問題中顯示的數據在整個對象中只有一個「Status」屬性,因此您不清楚數組中的內容。 – jfriend00

回答

0

你需要把JSON字符串使用JSON.parse一個JavaScript對象。請注意,舊版瀏覽器不支持JSON.parse(請參閱this Can I Usethis stack overflow question)。

var data = JSON.parse('{"Status":200,"Method":"SelectCompany","Errors":[],"Data":[{"Name":"MoreTimeAPITest","OrgNumber":"1234561234","APIKey":"168080BF-77A9-4B64-94C3-A5B3B4512A1C","Login":"moretime","Password":"SvMrqwk4G3"}],"DataType":{"Type":"list","ObjectType":"APIConnections"},"ResponseType":"json","API_Version":"1","API_Name":"PromikBook API","Server_Time":"2014-02-13 13:39:56.62275"}'); 

$.each(data, function(i, obj) { 
    console.log([obj.Status]); 

    newArr.push([obj.Status]); 
});