繁体   English   中英

JSON的反序列化,不匹配

[英]Deserialization of JSON, Mismatch

我的Spring MVC项目遇到了麻烦,该项目涉及使用API​​提取营养成分。 我继续得到一个嵌套异常是java.lang.IllegalArgumentException:参数类型不匹配。 下面发布的是JSON响应以及我为反序列化过程创建的类。

public FullResponse search() throws ClientProtocolException, IOException{

    CloseableHttpClient client = HttpClients.createDefault();
    HttpGet getProducts = new HttpGet("https://api.nutritionix.com/v1_1/search/"+ value +"?results=0%3A6&cal_min=0&cal_max=50000&fields=item_name%2Citem_id&appId=ac23ceb3&appKey=API_KEY");
    CloseableHttpResponse productResponse = client.execute(getProducts);
    String entityString = EntityUtils.toString(productResponse.getEntity());
    FullResponse test = new JSONDeserializer<FullResponse>  ().deserialize(entityString,FullResponse.class);

    return test;
}

//这是完整的响应类

public class FullResponse {

String total_hits;
String max_score;
List<Hits> hits;

public FullResponse(){

}

public String getTotal_hits() {
    return total_hits;
}

public void setTotal_hits(String total_hits) {
    this.total_hits = total_hits;
}

public String getMax_score() {
    return max_score;
}

public void setMax_score(String max_score) {
    this.max_score = max_score;
}

public List<Hits> getHits() {
    return hits;
}

public void setHits(List<Hits> hits) {
    this.hits = hits;
}

 }

//这是我创建的热门课程

public class Hits {

   String _index;
   String _type;
   String _id;
   String _score;
   List<Fields> fields;

 public Hits(){

   }

public String get_index() {
    return _index;
}

public void set_index(String _index) {
    this._index = _index;
}

public String get_type() {
    return _type;
}

public void set_type(String _type) {
    this._type = _type;
}

public String get_id() {
    return _id;
}

public void set_id(String _id) {
    this._id = _id;
}

public String get_score() {
    return _score;
}

public void set_score(String _score) {
    this._score = _score;
}

public List<Fields> getFields() {
    return fields;
}

public void setFields(List<Fields> fields) {
    this.fields = fields;
}


}

//这是我创建的我的领域类

public class Fields {

String item_name;

public Fields(){

}

public String getField(){
    return item_name;
}
public void setField(String name){
    item_name=name;
}
}

任何有关出问题的想法都会有很大的帮助。

这是请求中的JSON

{
"total_hits":11025,
"max_score":11.122117,
"hits":[{
"_index":"nixproductionv13",
"_type":"item",
"_id":"513fceb375b8dbbc210000e4",
"_score":11.122117,
"fields":{"item_name":"Whole Milk - 1 tbsp"}},

{"_index":"nixproductionv13",
"_type":"item",
"_id":"513fceb375b8dbbc2100017b",
"_score":10.7038355,
"fields":{"item_name":"2% Milk - 1 cup"}},

{"_index":"nixproductionv13",
"_type":"item",
"_id":"513fceb375b8dbbc210000f3",
"_score":10.7038355,
"fields":{"item_name":"1% Milk - 1 cup"}},

{"_index":"nixproductionv13",
"_type":"item",
"_id":"513fceb375b8dbbc210000fb",
"_score":10.689078,
"fields":{"item_name":"Skim Milk - 1 cup"}},

{"_index":"nixproductionv13",
"_type":"item",
"_id":"513fceb375b8dbbc210000e3",
"_score":10.65872,
"fields":{"item_name":"Whole Milk - 1 fl oz"}},

{"_index":"nixproductionv13",
"_type":"item",
"_id":"513fceb375b8dbbc2100017a",
"_score":10.392,
"fields":{"item_name":"2% Milk - 1 quart"}}]}

我不太了解flexjson ,但似乎不喜欢从JSON数字字段转换为Java String字段。 例如,当尝试反序列化此JSON数字时发生错误

"_score":10.7038355

进入此Java String字段

String _score;

如果将所有具有相应JSON数字字段的String字段更改为Double类型的字段,则代码将起作用。

另请注意, fields JSON对象

"fields":{"item_name":"Whole Milk - 1 tbsp"}},

是JSON对象,而不是JSON数组。 因此,您需要进行更改

List<Fields> fields;

Fields fields;

并适当地更改吸气剂和吸气剂。


另外,您可以使用其他的JSON解析库,例如Gson,它也支持这些标准类型转换。

暂无
暂无

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

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