Reputation: 103
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‚"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"}]
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.
Upvotes: 0
Views: 142
Reputation: 530
Your java environment may be serializing the array incorrectly.
Instead of ‚ - single bottom quote U+201A - you should have a comma (, U+002C).
Upvotes: 0
Reputation: 24116
Just looking at the first few lines:
[{"duration":12222‚"height":12‚
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 ‚
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 ‚
- 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.
Upvotes: 1
Reputation: 687
According to jsonlint.com, you should put values into ""
.
Because of the special character &
Upvotes: 1