簡體   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