简体   繁体   English

无法解析JSON数组字符串

[英]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. 我正在处理项目,我发送请求服务器获取JSON数组字符串。 I am using following java code to create one JSON array list string. 我正在使用以下java代码创建一个JSON数组列表字符串。

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: 在客户端,在AJAX响应中,我通过使用上面的方法获得跟随String:

[{"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: 当我使用JQuery解析它时,如下所示:

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

I am getting JS error as JSON.parse: expected property name or '}' 我收到JSON.parse: expected property name or '}' JS错误JSON.parse: expected property name or '}'

I am not getting why this error is occuring. 我不知道为什么会出现这个错误。

According to jsonlint.com, you should put values into "" . 根据jsonlint.com,您应该将值放入""

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. 这似乎不是有效的json。 duration is the key and the value is 12222 which is Integer , however you also have string data &sbquo; duration是关键,值是12222 ,即Integer ,但是你也有字符串数据&sbquo; next to the int, which makes this invalid json data. 在int旁边,这使得这个无效的json数据。

If you have mixed data, encapsulate it with double quote to treat it as a string. 如果您有混合数据,请使用双引号将其封装以将其视为字符串。


Update 更新

, is html encoded to &sbquo; ,是html编码为&sbquo; - there's your problem. - 这是你的问题。

Try this json string: 试试这个json字符串:

[
    {
        "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. 您的Java环境可能错误地序列化了数组。

Instead of ‚ - single bottom quote U+201A - you should have a comma (, U+002C). 而不是, - 单引号U + 201A - 你应该有一个逗号(,U + 002C)。

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

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