用getJSON休眠调用读取json对象序列。

问题描述 投票:0回答:1

我已经实现了一个REST服务,它提供了一个具有这种结构的json数据序列。

{
    "item":
            {
            "att1":"foo",
            "att2":"foo",
            "att3":"foo"
            }
}
{   
    "item":
            {
            "att1":"foo",
            "att2":"foo",
            "att3":"foo"
            }
}
{   
    "item":
            {
            "att1":"foo",
            "att2":"foo",
            "att3":"foo"
            }
}

我需要在javascript中用getJSON解析这些json数据,并打印出例如 "foo "的值。

我得到了SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 115 of the JSON data.

我使用的代码如下。

    $.getJSON("http://localhost:8080/rest/item", function(data) {
        $.each(data.item, function(index,element) {
            console.log(element.att1);
            console.log(element.att2);
            console.log(element.att3);
        })
    })

我认为getJSON不喜欢在不同对象后面加上逗号。

json rest getjson
1个回答
0
投票

你的json不应该是漂亮的打印。当括号内有空白、新行、制表符......时,许多解析器会抛出错误的exceptions。只要把你的json整理一下就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.