简体   繁体   English

JSON对象无法转换为JSON数组

[英]JSON Object can't be converted to JSON Array

ya i know that it's very usual problem while mapping but my problem is some different hear is the scenario when my response have the data it gives me JSON Response like this 是的,我知道映射时这是很常见的问题,但我的问题是不同的听到的是我的响应have the data向我提供JSON Response have the data时的情况

{
  "responseID": "110",
  "resultSet": [
    {
      "USERNAME": "Aninja",
      "position": "Developer",
      "salary": "60000"
    }
  ],
  "isSuccessful": true,
  "rtnCode": "0000"
}

and below is the same JSON response when data is not found 以下是when data is not found的相同JSON response

{
  "responseID": "123",
  "resultSet": {},
  "isSuccessful": true,
  "rtnCode": " "
}

as i can see hear when response have some data result set have JSON Array but when no data found we have JSON Object as a response 如我所见,当响应具有一些数据result set具有JSON Array但是当找不到数据时,我们将JSON Object作为响应

so this is the reason I'm getting this problem. 所以这就是我遇到这个问题的原因。 so my question is that how should i handle this problem thanks for your response 所以我的问题是,我应该如何处理这个问题,谢谢您的回复

Edit: the main problem is that i have made my model like list of JSON Object it works fine when there is result but it gives me error Can't convert JSON Object to JSON Array when result is empty s please suggest me how can i hanle it I'm using Jackson 2.2 i have also tried @JsonInclude(Include.NON_EMPTY) and @JsonInclude(Include.NON_NULL) 编辑:主要问题是我已经使我的模型像list of JSON Object一样在有结果时可以正常工作,但是给了我错误when result is empty Can't convert JSON Object to JSON Array when result is empty请建议我如何处理我正在使用Jackson 2.2我也尝试过@JsonInclude(Include.NON_EMPTY)@JsonInclude(Include.NON_NULL)

I wouldn't say it is mistake from server or back-end. 我不会说这是服务器或后端的错误。 But it is always a good practice to provide appropriate "Null Object Pattern" which describes the uses of such objects and their behavior. 但是,始终最好提供适当的“空对象模式”,以描述此类对象的用途及其行为。

So for better practice array which doesn't have any values should be sent back using "[]". 因此,为了更好的实践,应该使用“ []”将没有任何值的数组发送回去。 So in this case "resultSet" should be given as [] instead of {} so it can be easily understood at front-end. 因此,在这种情况下,应将“ resultSet”指定为[]而不是{},以便在前端轻松理解。

There are number of examples here which shows why it is useful to follow Null Object Pattern. 这里有许多示例,说明了遵循空对象模式很有用的原因。

For example, if you are returning count in you response and there is no count then it is better to use "0" instead of "null". 例如,如果您在响应中返回count而没有count,那么最好使用“ 0”而不是“ null”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM