0

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.

3
  • 1
    In deed there is a mistake in your JSON, paste your JSON here-> jsonlint.com You will see why Commented Apr 4, 2014 at 7:36
  • you are getting JSON it is javascript obj ... you did not need to parse it i think Commented Apr 4, 2014 at 7:37
  • Your JSON is not valid ... Commented Apr 4, 2014 at 7:40

3 Answers 3

1

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

Because of the special character &

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Do you have a source for that html-encoding? The online tools that I found don't HTML encode comma to &sbquo; and if they did encide it at all it should be &#44;
w3.org/wiki/Common_HTML_entities_used_for_typography (Single low quote) - perhaps he's wrong the wrong comma?
0

Your java environment may be serializing the array incorrectly.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.