英文:
How to add an empty JSON Array to JSON Object in a specific order
问题
以下是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"
}
希望这些翻译能帮助你理解JSON结构和相关的代码。如果你需要进一步的帮助,请随时提出问题。
英文:
I am trying to create this structure for a JSON -
{
"pricing": {
"quantity": 4200,
"minQuantity": 10,
"pricePerUnit": {
"amount": 0.2865,
"currency": "USD"
},
"discounts": []
},
"description": "test",
"guaranteedDeliveryTime": "testTime",
"offerType": "Currency"
}
I have came up with a solution that is pretty close. However the ordering seems a bit off.
Here is the code I am using -
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");
And the final output is as follows.
{
"guaranteedDeliveryTime": "testTime",
"description": "test",
"pricing": {
"quantity": 2342,
"minQuantity": 2342,
"discounts": [],
"pricePerUnit": {
"amount": 0.2865,
"currency": "USD"
}
},
"offerTime": "Currency"
}
The ordering seems off. I assume it is an issue as I am using a LinkedHashMap. I also tried setting the access order to see if that would change the ordering. Any advice would be greatly appreciated. Thanks.
答案1
得分: 1
A JSONObject is an unordered collection of name/value pairs.
https://www.javadoc.io/doc/org.json/json/20171018/org/json/JSONObject.html
英文:
A JSONObject is an unordered collection of name/value pairs.
https://www.javadoc.io/doc/org.json/json/20171018/org/json/JSONObject.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论