简体   繁体   中英

Not able to parse JSON array string

I am working on project in which I am sending request ot server for getting JSON array string. I am using following java code to create one JSON array list string.

JSONArray itemList = new JSONArray();
for(int i =0; i<4; i++) {
    String exe = "m";
    final JSONObject jsonObj = new JSONObject();
    jsonObj.put("filePath", 30);
    jsonObj.put("duration", 12222);
    jsonObj.put("bitRate", 1111);
    jsonObj.put("widht", 12);
    jsonObj.put("format", 123);
    jsonObj.put("height", 12);
    jsonObj.put("exe", exe);
    JSONObject jObject = new JSONObject();

    try {
        itemList.put(jsonObj);
        // jObject.put("itemList", itemList);
    } catch (Exception e) {
        System.out.println("ERROR");
    }
}
return itemList.toString();

On client side, in AJAX response, I am getting following String by using above method:

[{"duration":12222&sbquo;"height":12&sbquo;"widht":12&sbquo;"filePath":30&sbquo;"format":123&sbquo;"bitRate":1111&sbquo;"exe":"m"}&sbquo;{"duration":12222&sbquo;"height":12&sbquo;"widht":12&sbquo;"filePath":30&sbquo;"format":123&sbquo;"bitRate":1111&sbquo;"exe":"m"}&sbquo;{"duration":12222&sbquo;"height":12&sbquo;"widht":12&sbquo;"filePath":30&sbquo;"format":123&sbquo;"bitRate":1111&sbquo;"exe":"m"}&sbquo;{"duration":12222&sbquo;"height":12&sbquo;"widht":12&sbquo;"filePath":30&sbquo;"format":123&sbquo;"bitRate":1111&sbquo;"exe":"m"}]

When I am using JQuery to parse it as follows:

$jQ.each($jQ.parseJSON(responseJSON), function(idx, obj) {
    alert(obj.filePath);
});

I am getting JS error as JSON.parse: expected property name or '}'

I am not getting why this error is occuring.

According to jsonlint.com, you should put values into "" .

Because of the special character &

Just looking at the first few lines:

[{"duration":12222&sbquo;"height":12&sbquo;

This doesn't appear to be valid json. duration is the key and the value is 12222 which is Integer , however you also have string data &sbquo; next to the int, which makes this invalid json data.

If you have mixed data, encapsulate it with double quote to treat it as a string.


Update

, is html encoded to &sbquo; - there's your problem.

Try this json string:

[
    {
        "duration": 12222,
        "height": 12,
        "widht": 12,
        "filePath": 30,
        "format": 123,
        "bitRate": 1111,
        "exe": "m"
    },
    {
        "duration": 12222,
        "height": 12,
        "widht": 12,
        "filePath": 30,
        "format": 123,
        "bitRate": 1111,
        "exe": "m"
    },
    {
        "duration": 12222,
        "height": 12,
        "widht": 12,
        "filePath": 30,
        "format": 123,
        "bitRate": 1111,
        "exe": "m"
    },
    {
        "duration": 12222,
        "height": 12,
        "widht": 12,
        "filePath": 30,
        "format": 123,
        "bitRate": 1111,
        "exe": "m"
    }
]

this now validates.

Your java environment may be serializing the array incorrectly.

Instead of ‚ - single bottom quote U+201A - you should have a comma (, U+002C).

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