org.json.JSONException:JSONArray文本必须以' ['在1 [字符2行1] - 如何解决?

时间:2017-05-16 05:25:08

标签: hibernate spring-boot

JsonArray是:

{
  "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"
    }
  ]
}

使用springBoot的java代码如下:

  

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

     

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

     

JSONArray jsonArray = new JSONArray(data);

     

尝试{

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

}   
     

} catch(例外e){...   }

获得例外是: -

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.......

当我将json数组转换为JSONArray类然后获得异常。我如何处理JSON数组到JSONArray。

1 个答案:

答案 0 :(得分:0)

首先,您提供的JSON不是数组。只有"list"对象才是数组。

其次,"name"""的两个对象中"name": "pqr",的值应为"list"(如JSONObject)。所以提供的json无效。

对于有效的json,以下代码会从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); ..... ..... }
php