Spring-boot – org.json.JSONException: A JSONArray text must start with ‘[‘ at 1 [character 2 line 1] – how to solve

hibernatespring-boot

JsonArray Is:

{
  "list": [
    {
      "name": abc,
      "start_date": "2017-5-18 0:30:00",
      "end_date": "2017-6-1 0:30:00",
      "start_address": "Gujarat 380060, India",
      "end_address": "Ognaj, Ahmedabad, Gujarat, India"
    },
    {
      "name": pqr,
      "start_date": "2017-5-18 0:30:00",
      "end_date": "2017-6-1 0:30:00",
      "start_address": "Gujarat 380060, India",
      "end_address": "Ognaj, Ahmedabad, Gujarat, India"
    }
  ]
}

java code using springBoot is bellow

@RequestMapping(value = "/addabc", method = RequestMethod.POST)

@CrossOrigin
public Map addabc(@RequestBody String
data, HttpServletRequest request) {

JSONArray jsonArray=new JSONArray(data);

try{

for(int i=0; i < jsonArray.length(); i++) {
  JSONObject jsonobject = jsonArray.getJSONObject(i); 
   ......
   ......

}   

}catch (Exception e) { …
}

Getting exception is:-

org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
    at org.json.JSONTokener.syntaxError(JSONTokener.java:433) ~[json-20140107.jar:na]
    at org.json.JSONArray.<init>(JSONArray.java:105) ~[json-20140107.jar:na]
    at org.json.JSONArray.<init>(JSONArray.java:144) ~[json-20140107.jar:na]
    at com.abc.io.controller.abcController.addabc(abcController.java:214) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(Abstract.......

when i convert json array into JSONArray class then getting exception. How can i handle json array to JSONArray.

Best Answer

First, the JSON you given is not an array. Only the "list" object is an array.

Second, the value of "name" should be in "" (like "name": "pqr",) in both object in "list". So provided json is not valid.

For valid json following code is giving you the JSONObject from JSONArray

        JSONParser parser = new JSONParser();
        JSONObject json = (JSONObject) parser.parse(data);

        JSONArray jsonArray = (JSONArray) json.get("list");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonobject = (JSONObject) jsonArray.get(i);
            .....
            .....
        }
Related Topic