如何在jquery中将json数据传递给数组

时间:2014-02-14 06:56:05

标签: jquery json

我想把这个json变成一个数组。

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 :(得分:0)

您需要使用JSON.parse将JSON字符串转换为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]);
});