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

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

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 -


    {
      &quot;pricing&quot;: {
        &quot;quantity&quot;: 4200,
        &quot;minQuantity&quot;: 10,
        &quot;pricePerUnit&quot;: {
          &quot;amount&quot;: 0.2865,
          &quot;currency&quot;: &quot;USD&quot;
        },
        &quot;discounts&quot;: []
      },
      &quot;description&quot;: &quot;test&quot;,
      &quot;guaranteedDeliveryTime&quot;: &quot;testTime&quot;,
      &quot;offerType&quot;: &quot;Currency&quot;
    }

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&lt;String, Object&gt; pricePerUnit = new LinkedHashMap&lt;&gt;();
    pricePerUnit.put(&quot;amount&quot;, 0.2865);
    pricePerUnit.put(&quot;currency&quot;, &quot;USD&quot;);

    Map&lt;String, Object&gt; pricing = new LinkedHashMap&lt;&gt;(8, 0.6f, false);
    pricing.put(&quot;quantity&quot;, 2342);
    pricing.put(&quot;minQuantity&quot;, 2342);
    pricing.put(&quot;pricePerUnit&quot;, pricePerUnit);
    pricing.put(&quot;discounts&quot;, new JSONArray());

    JSONObject obj1 = new JSONObject();
    obj1.put(&quot;pricing&quot;, pricing);
    obj1.put(&quot;guaranteedDeliveryTime&quot;, &quot;testTime&quot;);
    obj1.put(&quot;description&quot;, &quot;test&quot;);
    obj1.put(&quot;offerTime&quot;, &quot;Currency&quot;);

And the final output is as follows.

    {
      &quot;guaranteedDeliveryTime&quot;: &quot;testTime&quot;,
      &quot;description&quot;: &quot;test&quot;,
      &quot;pricing&quot;: {
        &quot;quantity&quot;: 2342,
        &quot;minQuantity&quot;: 2342,
        &quot;discounts&quot;: [],
        &quot;pricePerUnit&quot;: {
          &quot;amount&quot;: 0.2865,
          &quot;currency&quot;: &quot;USD&quot;
        }
      },
      &quot;offerTime&quot;: &quot;Currency&quot;
    }

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:

确定