Unterminated string at character while parsing the JSON - java

I have a JSON which the jsonlint shows as a valid JSON but when I call
JSONObject rootObj=new JSONObject(orderJsonStr);
am getting org.json.JSONException: Unterminated string at character 13755, the place where it is break has "value": "12312&", it is breaking in place of '&'. Do we need to escape & or can we use a urlEncoder to encode the JSON?

For special characters in your json meesage like \ , # , & , # etc.. first convert them into their respective HEX value and then send your message.

Related

How to parse escaped Json using JSONObject in android

I'm unable to parse escaped json string through JSONObject, while trying to parse it, I always get an Unterminated Object error. But when I manually remove all of the escape characters from the json string, JSONObject parses it successfully.
But the problem is that for complex or nested json strings, I don't want to remove escape characters, because for nested structures, unescaping the characters results in an invalid json string.
This is the json string after removing escapes characters and I get unterminated object error due to invalid json when parsing through JSONObject
{"result":"unknown","message":"{"firebase":"unknown"}","sender":"unknown"}
This is the unescaped json string
{"result":"unknown","message":"{\"firebase\":\"unknown\"}","sender":"unknown"}
If you look at the json you are constructing it in wrong format.
In case of escaped json
{
"result":"unknown",
"message":"{"firebase":"unknown"}",
"sender":"unknown"
}
the json object "message" is having value "{" and firebase is causing exception.
In the the escaped json "message":{"firebase":"unknown"} will form valid json.

Is this a valid JSON string?

i am not familiar with json but as far as i know this should be a valid JSON string
{text:" Klan: Fury Elo: 9004 ",color:gold,extra:[{text:">>[ INFO ]<<",clickEvent:{action:run_command,value:"/klan info Fury"},hoverEvent:{action:show_text,value:"Zobrazis informaci o klanu."},color:gray,bold:true},{text:" "},{text:">>[ JOIN ]<<",color:green,bold:true,clickEvent:{action:run_command,value:"/klan join Fury"},hoverEvent:{action:show_text,value:"Podas zadost o vstoupeni do klanu."}}]}
But i am getting this error
Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 3
Can somebody explain where is an error?
It is not. Keys must be strings.
Whenever in doubt check out JSONLint - a great resource for format validation.
Color and extra are not wrapped around double quote. For more information, read https://en.m.wikipedia.org/wiki/JSON

Escape "[" bracket from json

I know that "[" brackets are used in json to specify a list such as:
"Value":["A","B"]
However I want to use those brackets as is in json like "Value":"[A TO B]"
since in the future this json string gets mapped to a URL:
I am using java where while using json parser it gives me an error.
Is there a way I can escape "[" brackets.
As long as you keep it in quotes, it is valid JSON. Try the code below in a validator
{
"key": "mykey",
"Value": "[A TO B]"
}
It returns: valid!

new JSONObject() lead to ERROR if json starts with [] instead of {} in Java/windows 7?

Does Apache JSONObject() only support json that begin and end under curly braces? I'm getting the following error:
org.apache.commons.json.JSONException: Expecting '{' on line 1, column 2 instead, obtained token: 'Token: [
Any pointers?
Thanks!
If it starts with a square bracket then it's not a JSON object, it's a JSON array.

Reading Json String using Gson results error "not a JSON Array"

In my project i have a complex json response. I want to read it by GSon.
JSON : {'FoodMenuRS':{'Results':[{'Items':{'Item':[{'#Id':'24'},{'#Id':'24'}]}}, {'Items':{'Item':{'#Id':'24'}}}]}}
It contains a JSONArray with first "Item" and JSONObject with second one. Hence its call results in error,
failed to deserialize json object {"#Id":"24"} given the type java.util.List<com.servlet.action.ItemInfo> and java.lang.IllegalStateException: This is not a JSON Array.
Please help how i should handle this scenario. Thanks.
The string you are showing is a JSONObject not a JSONArray. So, in this case you first of all have to get the JSONObject and perform further decoding on that JSONObject.
JSONObject - {}
JSONArray - []
And indeed JSONObject or JSONArray should be encoded using Double-quotes(")
Your JSON is valid, but not for the doble quotes (") because JSON supports simple quotes (') and no quotes in the key name. See http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Colle
However this JSON have key names that begin with #. For JSON strings this character is valid at the beginning of the name (see right column http://www.json.org/) but for Java this names are illegal (see Naming section http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html). Specifically, names started with # are annotations and you can't use annotations tags to declare variables, fields, methods, etc.
This is not a valid JSON object. Strings in JSON are always encapsulated in double quotes ("). Contact the producer of that JSON and tell him to use a correct encoder.

Categories

Resources