【问题标题】:org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1] - how to solve?org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1] - 如何解决?
【发布时间】:2017-10-15 01:02:15
【问题描述】:

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
公共地图 addabc(@RequestBody 字符串 数据,HttpServletRequest 请求){

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。

【问题讨论】:

  • 信息很清楚。您拥有的 JSON 不是 JSON 数组。数组以[ 开头。你所拥有的是一个 JSON 对象。它有一个名为“list”的属性,其类型是 JSON 数组。

标签: hibernate spring-boot


【解决方案1】:

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

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

对于有效的 json,以下代码为您提供来自 JSONArrayJSONObject

        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);
            .....
            .....
        }

【讨论】:

  • 无法将 JsonArray 转换为 JsonObject
  • @SecondView JSONArray.get(i) 返回一个Object。所以你不可能做到这一点!
  • 但是当我尝试你的方式时遇到了一个异常,它说不能投射
  • 然后创建一个新问题!
猜你喜欢
  • 1970-01-01
  • 2022-01-22
  • 2016-05-16
  • 2021-06-17
  • 1970-01-01
  • 2015-03-05
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
相关资源
最近更新 更多