javascript - JSON not well formed error - Stack Overflow

In the below code,<head><meta charset="UTF-8"><title>JSON ex<title>&

In the below code,

<head>
        <meta charset="UTF-8">
        <title>JSON ex</title>
        <script type = "text/javascript" 
            src = ".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

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

I see JSON not well formed error. result.json sits in the same folder.

Due to this error, accountGrid goes empty.

What does it mean to say not well formed error, on execution of jQuery.getJSON?

In the below code,

<head>
        <meta charset="UTF-8">
        <title>JSON ex</title>
        <script type = "text/javascript" 
            src = "http://ajax.googleapis./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

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

I see JSON not well formed error. result.json sits in the same folder.

Due to this error, accountGrid goes empty.

What does it mean to say not well formed error, on execution of jQuery.getJSON?

Share Improve this question asked Jan 6, 2016 at 13:51 overexchangeoverexchange 1 1
  • 2 There are online/offline validators that you can use to validate your JSON. – Drumbeg Commented Jan 6, 2016 at 13:57
Add a ment  | 

1 Answer 1

Reset to default 6

Let's consider the grammar of JSON...

JSON must be a single value (object, array, etc.)

A JSON object should be a single value. What you have is two objects separated by a ma. 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.

# 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. The spec says that value (defined above) can be an array:

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

So your JSON would look like this:

[
    {
        "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)

If you represent your data as an array, your callback should simply assign the resulting parsed JSON to the accountGrid variable:

<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:

accountGrid = accountGrid.concat(entry);

______

Writing JSON in the future

I suggest you edit JSON files within an IDE that supports syntax highlighting for JSON, then these problems would be raised within the editor. Or, use one of the many online tools available.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742408597a4438355.html

相关推荐

  • javascript - JSON not well formed error - Stack Overflow

    In the below code,<head><meta charset="UTF-8"><title>JSON ex<title>&

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信