我正在尝试为 JSON 创建这个结构 -
{
"pricing": {
"quantity": 4200,
"minQuantity": 10,
"pricePerUnit": {
"amount": 0.2865,
"currency": "USD"
},
"discounts": []
},
"description": "test",
"guaranteedDeliveryTime": "testTime",
"offerType": "Currency"
}
我想出了一个非常接近的解决方案。然而,顺序似乎有点偏离。 这是我正在使用的代码 -
Map<String, Object> pricePerUnit = new LinkedHashMap<>();
pricePerUnit.put("amount", 0.2865);
pricePerUnit.put("currency", "USD");
Map<String, Object> pricing = new LinkedHashMap<>(8, 0.6f, false);
pricing.put("quantity", 2342);
pricing.put("minQuantity", 2342);
pricing.put("pricePerUnit", pricePerUnit);
pricing.put("discounts", new JSONArray());
JSONObject obj1 = new JSONObject();
obj1.put("pricing", pricing);
obj1.put("guaranteedDeliveryTime", "testTime");
obj1.put("description", "test");
obj1.put("offerTime", "Currency");
最终输出如下
{
"guaranteedDeliveryTime": "testTime",
"description": "test",
"pricing": {
"quantity": 2342,
"minQuantity": 2342,
"discounts": [],
"pricePerUnit": {
"amount": 0.2865,
"currency": "USD"
}
},
"offerTime": "Currency"
}
订单似乎不对。我认为这是一个问题,因为我正在使用 LinkedHashMap。我还尝试设置访问顺序以查看是否会更改顺序。任何建议将不胜感激。谢谢。