this is JSON string 1
{"title":["1","2"], "amount":["1","2"]}
this is JSON string 2
{"title":"", "amount":""}
string 1 is created when I enter values in form and string 2 is created when I dont,
I want to know if the string is in format 1 that is title is an array [“1”, “2”] or format 2 that is title is just a string “” on the server side in a servlet, before I parse it. is there any way of doing so?
this is my previous question,
How do I parse this JSON string using GSON in servlet
which is solved but as you can see there i have class Data which has instance variables of type ArrayList, so when I parse it with this line
Data data = gson.fromJson(param, Data.class);
it throws exception
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 24
because as I have declared ArrayList, it expects array only in json to parse it without any exceptions….but when I dont enter values in my form it doesnt create json string as
{"title":[], "amount":[]}
rather it creates like this
{"title":'', "amount":''}
which has string as value, which causes parsing to throw exception
Check Google GSON it allows you to parse JSON server side.
It goes something like this:
If, for example, I run the following example code:
I get as output:
If I understood you correctly I think it suits you 100%
UPDATE
Since you are trying to deserialize your JSON string directly to a Data object, if you want to keep doing that direct deserialization you have to use a custom deserialization mechanism