【问题标题】:JSON mapping exceptionJSON 映射异常
【发布时间】:2016-01-05 11:34:57
【问题描述】:

我正在尝试将 json 数据发送到我的 JAVA 类但出现错误

“执行 POST IntakeFormSections/PostData 失败:org.jboss.resteasy.spi.ReaderException: org.codehaus.jackson.map.JsonMappingException: 无法将 org.json.JSONObject 的实例反序列化出 START_ARRAY 令牌”

控制器:

var datanew =  angular.toJson($scope.data);
 $http({

url :"http://localhost:8080/......IntakeFormSections/PostData",

      method: 'POST',
      data : datanew,
      headers: {'Content-Type': 'application/Json'},

      })};

JAVA代码:

@POST
@Path("/PostData")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)

public String UpdateSiteVisitInfo(JSONObject data) throws JSONException {
System.out.println("++++++++++");
System.out.println(data);
return null;}

【问题讨论】:

  • 请求时datanew的内容是什么?
  • datanew 的内容是这个 json:- "questions": [{ "Id": 1, "description": "Submitter Contact Information", "type": "hidden", "required": "false", "subquestions1": [{ "Id": "a", "description": "ID", "type": "text", "name": "id", "patternType": "alphanumeric", “必需”:“假”},{“Id”:“b”,“描述”:“名称”,“类型”:“文本”,“名称”:“用户”,“模式类型”:“文本”, “必需”:“假”,“长度”:100 }]

标签: java angularjs json resteasy


【解决方案1】:

JSON 格式错误。

应该是这样的。

[{ "Id": 1, "description": "Submitter Contact Information", "type": "hidden", "required": "false", "subquestions1": [{ "Id": "a", "description": "ID", "type": "text", "name": "id", "patternType": "alphanumeric", "required":"false" }, { "Id": "b", "description": "Name", "type": "text", "name": "user", "patternType": "text", "required":"false", "length":100 }]}]

【讨论】:

    【解决方案2】:

    您将 JSONObjects 列表作为 datanew 传递,但在控制器中它被映射到单个 JSONObject,请更改控制器参数类型以接受 JSONObjects 列表。

    【讨论】:

      【解决方案3】:

      正如所指出的,您正在发送一个包含一个 JSONObject 元素的数组。这意味着您要么应该有一个 JSONObject[] 数组的参数,然后检索第一个元素,要么将单个对象发送到端点。

      【讨论】:

        猜你喜欢
        • 2023-04-08
        • 2020-11-13
        • 2022-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-29
        • 2012-11-15
        • 1970-01-01
        相关资源
        最近更新 更多