如何按特定顺序将空的JSON数组添加到JSON对象中

huangapple go评论100阅读模式
英文:

How to add an empty JSON Array to JSON Object in a specific order

问题

以下是JSON结构的翻译部分:

  1. {
  2. "pricing": {
  3. "quantity": 4200,
  4. "minQuantity": 10,
  5. "pricePerUnit": {
  6. "amount": 0.2865,
  7. "currency": "USD"
  8. },
  9. "discounts": []
  10. },
  11. "description": "test",
  12. "guaranteedDeliveryTime": "testTime",
  13. "offerType": "Currency"
  14. }

以下是代码部分的翻译:

  1. Map<String, Object> pricePerUnit = new LinkedHashMap<>();
  2. pricePerUnit.put("amount", 0.2865);
  3. pricePerUnit.put("currency", "USD");
  4. Map<String, Object> pricing = new LinkedHashMap<>(8, 0.6f, false);
  5. pricing.put("quantity", 2342);
  6. pricing.put("minQuantity", 2342);
  7. pricing.put("pricePerUnit", pricePerUnit);
  8. pricing.put("discounts", new JSONArray());
  9. JSONObject obj1 = new JSONObject();
  10. obj1.put("pricing", pricing);
  11. obj1.put("guaranteedDeliveryTime", "testTime");
  12. obj1.put("description", "test");
  13. obj1.put("offerTime", "Currency");

以下是最终输出的翻译部分:

  1. {
  2. "guaranteedDeliveryTime": "testTime",
  3. "description": "test",
  4. "pricing": {
  5. "quantity": 2342,
  6. "minQuantity": 2342,
  7. "discounts": [],
  8. "pricePerUnit": {
  9. "amount": 0.2865,
  10. "currency": "USD"
  11. }
  12. },
  13. "offerTime": "Currency"
  14. }

希望这些翻译能帮助你理解JSON结构和相关的代码。如果你需要进一步的帮助,请随时提出问题。

英文:

I am trying to create this structure for a JSON -

  1. {
  2. &quot;pricing&quot;: {
  3. &quot;quantity&quot;: 4200,
  4. &quot;minQuantity&quot;: 10,
  5. &quot;pricePerUnit&quot;: {
  6. &quot;amount&quot;: 0.2865,
  7. &quot;currency&quot;: &quot;USD&quot;
  8. },
  9. &quot;discounts&quot;: []
  10. },
  11. &quot;description&quot;: &quot;test&quot;,
  12. &quot;guaranteedDeliveryTime&quot;: &quot;testTime&quot;,
  13. &quot;offerType&quot;: &quot;Currency&quot;
  14. }

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 -

  1. Map&lt;String, Object&gt; pricePerUnit = new LinkedHashMap&lt;&gt;();
  2. pricePerUnit.put(&quot;amount&quot;, 0.2865);
  3. pricePerUnit.put(&quot;currency&quot;, &quot;USD&quot;);
  4. Map&lt;String, Object&gt; pricing = new LinkedHashMap&lt;&gt;(8, 0.6f, false);
  5. pricing.put(&quot;quantity&quot;, 2342);
  6. pricing.put(&quot;minQuantity&quot;, 2342);
  7. pricing.put(&quot;pricePerUnit&quot;, pricePerUnit);
  8. pricing.put(&quot;discounts&quot;, new JSONArray());
  9. JSONObject obj1 = new JSONObject();
  10. obj1.put(&quot;pricing&quot;, pricing);
  11. obj1.put(&quot;guaranteedDeliveryTime&quot;, &quot;testTime&quot;);
  12. obj1.put(&quot;description&quot;, &quot;test&quot;);
  13. obj1.put(&quot;offerTime&quot;, &quot;Currency&quot;);

And the final output is as follows.

  1. {
  2. &quot;guaranteedDeliveryTime&quot;: &quot;testTime&quot;,
  3. &quot;description&quot;: &quot;test&quot;,
  4. &quot;pricing&quot;: {
  5. &quot;quantity&quot;: 2342,
  6. &quot;minQuantity&quot;: 2342,
  7. &quot;discounts&quot;: [],
  8. &quot;pricePerUnit&quot;: {
  9. &quot;amount&quot;: 0.2865,
  10. &quot;currency&quot;: &quot;USD&quot;
  11. }
  12. },
  13. &quot;offerTime&quot;: &quot;Currency&quot;
  14. }

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

huangapple
  • 本文由 发表于 2023年4月10日 21:14:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定