如何将此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对象
答案 0 :(得分:0)
你的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并粘贴它here以获得pojo。
答案 1 :(得分:0)
这是一个非常常见的问题,称为数据封送。在java中,杰克逊是一般的最佳解决方案。阅读本教程:http://wiki.fasterxml.com/JacksonInFiveMinutes
答案 2 :(得分:0)
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
答案 3 :(得分:0)
如果我对“d”键进行子串,并将其映射为JSON数组,那么它必须是现在的解决方案......