Java将JSON对象或JSON数组转list对象 public ResultJson validateInventury(@RequestBody String jsonObject)
第⼀种对象参数
{"inventoryDTOs":[{
"type": 0,
"saleQuantity": 188.0000,
"basePrice":1000.00,
"salePrice": 3000.00,
"inventoryId": 35
json转换对象}, {
"type": 0,
"saleQuantity": 949.0000,
"basePrice":200.00,
"salePrice": 300.00,
"inventoryId": 28
},{
"type": 1,
"saleQuantity": 94.0000,
"basePrice":200.00,
"salePrice": 1000.00,
"inventoryId": 326
}
]}
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
转换:
import com.alibaba.fastjson.JSONObject;
JSONObject jsonObject = JSONObject.parseObject(jsonObject);
String versionInfoStr = String("inventoryDTOs");
// 数组
List<InventoryDTO> inventoryDTOs = JSON.parseArray(versionInfoStr, InventoryDTO.class);
/
/ 对象
InventoryDTO dto =JSON.parseObject(xx,InventoryDTO.class);
=================================================================
=================================================================
第⼆种json数组参数:
[
{
"adminId": 360,
"createTimeStr": "2021-10-28 18:29:44",
"orderSn": "SZ20211028SH0018",
"relationId": 21334,
"status": 14,
"type": 0
}
]
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
转换
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
public static void main(String[] args)
{
String string = "[{.....}]";
JSONArray jsonArray = JSONArray.fromObject(string);
List<Todo> todos = List(jsonArray, new Todo() , new JsonConfig()); }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论