[英]Mapping JSON Array to Java POJO
我將如何將此 JSON 映射到對象?
{"d":"[{\"Key\":\"3\",\"ExternalKey\":\"143234A\",\"Name\":\"cup of juice\",\"Litres\":\"2 litres\",\"Date\":\"2016-10-06T08:32:27\",\"Capacity\":5.4900,\"CapacityType\":\"%\"}, {\"Key\":\"3\",\"ExternalKey\":\"143234A\",\"Name\":\"cup of milk\",\"Litres\":\"2.4 litres\",\"Date\":\"2016-10-06T08:32:27\",\"Capacity\":1667.6100,\"CapacityType\":\"M\"}]"}
我試過使用 HashMap,但它只是將“d”作為字符串,其余的作為帶有一個元素的 String 對象
您的 Json 字符串有一些額外的 " 字符。
這是最終的json:
{
"d": [
{
"Key": "3",
"ExternalKey": "143234A",
"Name": "cup of juice",
"Litres": "2 litres",
"Date": "2016-10-06T08:32:27",
"Capacity": 5.49,
"CapacityType": "%"
},
{
"Key": "3",
"ExternalKey": "143234A",
"Name": "cup of milk",
"Litres": "2.4 litres",
"Date": "2016-10-06T08:32:27",
"Capacity": 1667.61,
"CapacityType": "M"
}
]
}
現在您可以復制 json 並將其粘貼到此處以獲取 pojo。
這是一個非常常見的問題,稱為數據封送處理。 在 Java 中,Jackson 是通用的最佳解決方案。 閱讀本教程: http : //wiki.fasterxml.com/JacksonInFiveMinutes
There are n numbers of libraries available for parsing the JSON and convert into in java classes. Some of the examples are, GSON Jackson Logan Square Moshi, etc You need to create a java class which map with your JSON Response and need to change your pojo according to parsing library. For Example, Using Logan Square you must annotate your POJO class to @JsonObject and all properties must be annotated with @JsonField. Now, you can parse your json using method ... LoganSquare.parse(jsonString, pojoClassName); ... Hopefully this will help you. Cheers
如果我將“d”鍵子串出來並將其映射為 JSON 數組,那么它就可以工作了,這將是現在的解決方案......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.