Issue
I have created a JSON object manually using the data obtained in java script and sent the JSON Object to a servlet using Ajax.
The Object is able to receive but unable to convert to JSON Object again. I am using json-simple-1.1.jar .
JSONObject json=(JSONObject)new JSONParser().parse("json_data");
But ended with the following error
Aug 31, 2015 2:28:13 AM source.main.UpdateDetails doGet
SEVERE: null
Unexpected character (j) at position 0.
at org.json.simple.parser.Yylex.yylex(Yylex.java:610)
at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
at source.main.UpdateDetails.processRequest(UpdateDetails.java:55)
at source.main.UpdateDetails.doGet(UpdateDetails.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
The output of JSON File sent as request is as follows :
{
"0":
{
"card_name":"Pallela.Manga",
"aadhar_eid":"1046106160065750110728131521",
"aadhar_uid":"693228374919",
"relation":"SELF",
"age":"43"
},
"1":
{
"card_name":"KondaBabu",
"aadhar_eid":"1046106160008020110728125714",
"aadhar_uid":"996251988555",
"relation":"HUSBAND",
"age":"47"
},
"2":
{
"card_name":"SrinivasaRao",
"aadhar_eid":"1046106125348220110728131743",
"aadhar_uid":"609986909901",
"relation":"SON","age":"25"
},
"3":
{
"card_name":"Ganesh",
"aadhar_eid":"1046106160002957110728132026",
"aadhar_uid":"603873912563",
"relation":"SON",
"age":"23"
}
}
Please help me out of this.. Thank you
Solution
previously I created a JSON String and parsed it to JSON Object using JSON.parse(json_string);
and i've sent the same to the servlet. The servlet trying to read it but as it is got the object as string it is printing as [object object] and while trying to parse back to JSON String giving above mentioned error
finally i got help from stackoverflow from another post related How to read json sent by ajax in servlet
I made a string out of json using the below and sent to the servlet.
JSON.stringify(JSONobj)
The servlet received the string and created the JSON Object successfully using
JSONObject json = (JSONObject) new JSONParser().parse("json_data");
sorry if my english is bad... Thank you
Answered By - Mallesh
Answer Checked By - Senaida (JavaFixing Volunteer)