Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么这个JSON没有解析?_Javascript_Json - Fatal编程技术网

Javascript 为什么这个JSON没有解析?

Javascript 为什么这个JSON没有解析?,javascript,json,Javascript,Json,我从服务器收到一些测试数据: var data = [ '{"i":5,"t":3,"f":3,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}', '{"i":6,"t":1,"f":3,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}', '{"i":11,"t"

我从服务器收到一些测试数据:

var data = [ '{"i":5,"t":3,"f":3,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":6,"t":1,"f":3,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":11,"t":3,"f":3,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":14,"t":2,"f":3,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":16,"t":1,"f":3,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":23,"t":1,"f":3,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":26,"t":2,"f":3,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":27,"t":2,"f":3,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":30,"t":2,"f":3,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":31,"t":3,"f":3,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":4,"t":3,"f":2,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":10,"t":3,"f":2,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":12,"t":3,"f":1,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":13,"t":3,"f":1,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":15,"t":3,"f":2,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":17,"t":3,"f":2,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":18,"t":3,"f":2,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}',
                                    '{"i":20,"t":3,"f":1,"m":null,"h":0,"c":"2016-02-28T22:30:56.000Z"}' ];
它是从以下服务器生成的:

json_response.push(JSON.stringify({i : [some value], t:  [some value], f:  [some value], m:  [some value], h:  [some value], c :  [some value]}));
});
我知道你想访问其中一些数据。因此,请尝试访问第一个元素:

console.log(data[0].i); //gives undefined
然后我尝试解析数据:

var json_parse = JSON.parse(data);
console.log(data[0].i); //gives undefined
但这会产生一个错误,即存在一个
syntaxError:Unexpected token

我看不出哪里应该有逗号

我希望能够访问此json数组中的值。

尝试以下操作:

var json_parse = JSON.parse(data[0]);
console.log(json_parse.i);
并切换服务器以生成响应,如下所示:

json_response.push({i : [some value], t:  [some value], f:  [some value], m:  [some value], h:  [some value], c :  [some value]});
试试这个:

var json_parse = JSON.parse(data[0]);
console.log(json_parse.i);
并切换服务器以生成响应,如下所示:

json_response.push({i : [some value], t:  [some value], f:  [some value], m:  [some value], h:  [some value], c :  [some value]});

无法将数组传递给JSON.parse。您拥有的是一个JSON字符串数组。一个简单的解决方案是解析数组中的每个项

var data=['{“i”:5,“t”:3,“f”:3,“m”:null,“h”:0,“c”:“2016-02-28322:30:56.000Z”},
“{i:6,t:1,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:11,t:3,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:14,t:2,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:16,t:1,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:23,t:1,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:26,t:2,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:27,t:2,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:30,t:2,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:31,t:3,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:4,t:3,f:2,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:10,t:3,f:2,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:12,t:3,f:1,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:13,t:3,f:1,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:15,t:3,f:2,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:17,t:3,f:2,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:18,t:3,f:2,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:20,t:3,f:1,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”
];
//对于数组中的每个元素
var data2=data.map(jsonString=>JSON.parse(jsonString));
//单解析
var data3=JSON.parse('['+data.join(',')+']');
console.log(数据2[0].i);//5.

console.log(数据3[0].i);//5
不能将数组传递给JSON.parse。您拥有的是一个JSON字符串数组。一个简单的解决方案是解析数组中的每个项

var data=['{“i”:5,“t”:3,“f”:3,“m”:null,“h”:0,“c”:“2016-02-28322:30:56.000Z”},
“{i:6,t:1,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:11,t:3,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:14,t:2,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:16,t:1,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:23,t:1,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:26,t:2,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:27,t:2,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:30,t:2,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:31,t:3,f:3,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:4,t:3,f:2,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:10,t:3,f:2,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:12,t:3,f:1,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:13,t:3,f:1,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:15,t:3,f:2,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:17,t:3,f:2,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:18,t:3,f:2,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”,
“{i:20,t:3,f:1,m:null,h:0,c:“2016-02-28222:30:56.000Z”}”
];
//对于数组中的每个元素
var data2=data.map(jsonString=>JSON.parse(jsonString));
//单解析
var data3=JSON.parse('['+data.join(',')+']');
console.log(数据2[0].i);//5.
console.log(数据3[0].i);//5
注意点

1) 问题中的
数据
是一个数组
[]
,它包含多个字符串,即:整个字符串化的多个对象

所以在这种情况下,
data[0]
是有效的,而不是
data[0]。i

2) 这一行
var json\u parse=json.parse(数据)是错误的。您正在尝试解析已经是数组的变量。您只能解析JSON,在本例中,它不是JSON,而是数组

所以这是正确的
var json_parse=json.parse(数据[0])

因为您说过您将有多个对象,并且您无法解析数据中的每个索引,所以您可以尝试下面的逻辑

var parsedData = data.map(eachIndex=> JSON.parse(eachIndex));
console.log(parsedData[0].i);
值得注意的是

1) 问题中的
数据
是一个数组
[]
,它包含多个字符串,即:整个字符串化的多个对象

所以在这种情况下,
data[0]
是有效的,而不是
data[0]。i

2) 这一行
var json\u parse=json.parse(数据)是错误的。您正在尝试解析已经是数组的变量。您只能解析JSON,在本例中,它不是JSON,而是数组

所以这是正确的
var json_parse=json.parse(数据[0])

因为您说过您将有多个对象,并且您无法解析数据中的每个索引,所以您可以尝试下面的逻辑

var parsedData = data.map(eachIndex=> JSON.parse(eachIndex));
console.log(parsedData[0].i);

它起作用了!有没有一种方法可以让我不解析每个json对象?服务器或客户端代码的更改?我这样问是因为一些数组中会包含很多对象,如果我需要解析每个对象,我会考虑性能问题。解析发生在客户端,可能是一些旧的移动电话,为了增加切换响应生成的原因,现在的数据是一个字符串数组。请注意,每个条目都由一个引号包围,您希望