简体   繁体   中英

Uncaught SyntaxError: Unexpected token [ with JSON.parse

I am trying to parse a string, coming from HTTP. The string looks like

[
    {
        "apiVersion": "ocs.openshift.io/v1alpha1",
        "kind": "StorageCluster",
        "metadata": {
            "name": "example-storagecluster",
            "namespace": "openshift-storage"
        },
        "spec": {
            "manageNodes": false
        }
    },

 ]

Parsing this string gives this error:

Uncaught SyntaxError: Unexpected token [ with JSON.parse

Can someone help me how to parse this string and get the value inside [ ]. Thanks in advance.

最后一个}之后的逗号是不允许的。

Here is the code. I executed in background script and it gives values correctly

var string = '[{"UserID":"10001","Name":"Ram"},{"UserID":"10002","Name":"Sultana"},{"UserID":"10003","Name":"Lakshmi"}]';


var parser = new JSONParser();
var parsedData = parser.parse(string);
var length = parsedData.length;

gs.print(length);



for(var i=0;i<length;i++){

gs.print(parsedData[i].UserID); 
gs.print(parsedData[i].Name);

}

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.

That is not a valid JSON document. You can always use https://jsonlint.com/ to validate your documents if you don't have any other way.

In your case you have 1 comma too much in your JSON document:

[
    {
        "apiVersion": "ocs.openshift.io/v1alpha1",
        "kind": "StorageCluster",
        "metadata": {
            "name": "example-storagecluster",
            "namespace": "openshift-storage"
        },
        "spec": {
            "manageNodes": false
        }
    }
]

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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