Archimedes Archimedes - 4 months ago 15
JSON Question

Blank space in JSON Value

I get a string in jsonformat and I try to get the values. My method works fine, but if the value has a blank space in it, my method cracks up and it rains exceptions

private String getValue(String jsonval)
{
try
{
JSONObject jsonObject = new JSONObject(jsonval);
return jsonObject.optString("id");
}
catch(JSONException e)
{
e.printStackTrace();
}
return null;
}


While this string works fine:
{titleDE=Deutschland, id=10, titleEN=Germany}

This one makes problems:
{titleDE=Costa Rica, id=10, titleEN=Costa Rica}

The exceptions says that the blank space is a unterminated object...

Any suggestions or hints?

Answer

Send key and values in JSON string encased within double inverted quotes. Like this - "key"="string".

{"titleDE"="Deutschland", "id"=10, "titleEN"="Germany"}
{"titleDE"="Costa Rica", "id"=10, "titleEN"="Costa Rica"}

Also observe there is no blank space between key and = OR = and value. Integer/boolean values can be without quotes.