简体   繁体   English

JSON格式错误

[英]JSON not well formed error

In the below code, 在下面的代码中,

<head>
        <meta charset="UTF-8">
        <title>JSON ex</title>
        <script type = "text/javascript" 
            src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
        </script>

        <script type="text/javascript" language = "javascript">
            var accountGrid = [];
            $(document).ready(function(){
                $.getJSON('result.json', function(entry){
                    accountGrid.push({
                        name: entry.name,
                        marketValue: entry.marketValue,
                        cash: entry.cash,
                        legend: entry.legend
                    }); 
                });
            });
        </script>
    </head>
    <body>

    </body>

result.json result.json

{
    'name': 'Account1',
    'marketValue': '10990',
    'cash': '199926',
    'legend': 'orange'
},
{
    'name': 'Account2',
    'marketValue': '156590',
    'cash': '133856',
    'legend': 'darkorange'
}

I see JSON not well formed error. 我看到JSON not well formed错误。 result.json sits in the same folder. result.json位于同一文件夹中。

Due to this error, accountGrid goes empty. 由于此错误, accountGrid空。

What does it mean to say not well formed error, on execution of jQuery.getJSON ? 在执行jQuery.getJSONnot well formed错误的错误是什么意思?

Let's consider the grammar of JSON ... 让我们考虑一下JSON语法 ...

JSON must be a single value (object, array, etc.) JSON必须是单个值(对象,数组等)

A JSON object should be a single value. JSON对象应为单个值。 What you have is two objects separated by a comma. 您所拥有的是两个用逗号分隔的对象。 Perhaps you could assign each of these as members of a single object. 也许您可以将每个对象分配为单个对象的成员。

# a JSON object consists of a single value definition
value
    string
    number
    object
    array
    true
    false
    null

# this value can be an object
object
    {}
    { members }

# or an array
array
    []
    [ elements ]

______ ______

Single-quotation marks are illegal 单引号是非法的

JSON forbids the use of single-quotes ( ' ) for strings. JSON禁止对字符串使用单引号( ' )。

# a string consists of chars wrapped in double-quotation marks
string
    ""
    " chars "

So replace all single quotes to double quotation marks. 因此,将所有单引号替换为双引号。

The above two things considered, you will end up with something like this: 考虑以上两件事,您将得到如下结果:

{
    "key1": {
        "name": "Brokerage Account 3",
        "marketValue": "1999990",
        "cash": "1995826",
        "legend": "orange"
    },
    "key2": {
        "name": "Account 3",
        "marketValue": "1949990",
        "cash": "1695856",
        "legend": "darkorange"
    }
}

[ Try it out online ] [ 在线试用 ]

______ ______

Place objects within an array 将对象放在数组中

Alternatively, as suggested by @Gerald Schneider, place the objects within an array. 或者,按照@Gerald Schneider的建议,将对象放置在数组中。 The spec says that value (defined above) can be an array : 规范说value (上面定义)可以是一个array

array
    []
    [ elements ] # elements can multiple values, e.g. objects

So your JSON would look like this: 因此,您的JSON如下所示:

[
    {
        "name": "Account1",
        "marketValue": "10990",
        "cash": "199926",
        "legend": "orange"
    },
    {
        "name": "Account2",
        "marketValue": "156590",
        "cash": "133856",
        "legend": "darkorange"
    }
]

[ Try it out online ] [ 在线试用 ]

______ ______

Consuming the parsed JSON (where the JSON is an array) 消耗解析后的JSON (其中JSON是数组)

If you represent your data as an array, your callback should simply assign the resulting parsed JSON to the accountGrid variable: 如果将数据表示为数组,则回调应仅将生成的已解析JSON分配给accountGrid变量:

<script type="text/javascript" language = "javascript">
    var accountGrid = [];
    $(document).ready(function(){
        $.getJSON('result.json', function(entry){
            accountGrid = entry;
        });
    });
</script>

Alternatively, if you wanted to append the values of entry to accountGrid : 另外,如果您想将entry的值附加到accountGrid

accountGrid = accountGrid.concat(entry);

______ ______

Writing JSON in the future 将来编写JSON

I suggest you edit JSON files within an IDE that supports syntax highlighting for JSON, then these problems would be raised within the editor. 我建议您在支持JSON语法高亮显示的IDE中编辑JSON文件,然后在编辑器中引发这些问题。 Or, use one of the many online tools available . 或者, 使用许多可用的在线工具之一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM