【发布时间】:2016-12-09 13:01:35
【问题描述】:
所以这是我在尝试运行 JSON.parse 之前返回的 JSON.stringify
{"id":"2","name":"<small>L</small>(+)-Amethopterin Hydrate","class":"6.1","subclass":"","packing_group":"III","un":"2811","cas":"133073-73-1","poisons":"","hazardous":"Yes","restricted":"No","epa":"","added_by":"0","carcinogen":null},
{"id":"3","name":"(+)-Biotin 4-Nitrophenyl ester","class":"","subclass":"","packing_group":"","un":"","cas":"33755-53-2","poisons":"","hazardous":"No","restricted":"No","epa":"","added_by":"0","carcinogen":null},
{"id":"4","name":"(+)-Biotin N-hydroxysuccinimide ester","class":"","subclass":"","packing_group":"","un":"","cas":"35013-72-0","poisons":"","hazardous":"No","restricted":"No","epa":"","added_by":"0","carcinogen":null}
当我尝试 JSON.parse 时,我得到Unexpected end of JSON input。而且我不能将它作为 JSON 对象访问,因为它会说 can't define id 或类似的东西。
JSON.parse(this.searchService.searchJson(this.php_url));
this.searchService.searchJson(this.php_url) 基本上就是我的 JSON 字符串。如上所述给出错误。
此外,如果我只是尝试对 3 个元素中的 1 个进行字符串化,它会给我Unexpected token u in JSON at position 0
调用函数:
searchJson(url: any): any
{
let items: any = [];
let new_data: any = [];
$.getJSON(url ,
function(data)
{
let temp_items: any = {};
console.log(data);
$.each(data, function (key, val)
{
new_data.push(JSON.stringify(val));
});
});
return new_data;
}
【问题讨论】:
-
介意给我们看一下解析代码吗?
-
@TheLazySloth 更新了它。返回的基本上是一个字符串化的数组。
-
就是用逗号写的3个对象..应该是数组什么的..jsfiddle.net/c8743h70
-
在 JSONLint.com 输入 JSON 并显示错误。将其添加到 [ ] 中,错误消失了。试一试?
-
@echonax 问题是......我很确定它们在一个数组中,我会用我的调用函数更新
标签: javascript json angular typescript