You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按指定顺序向JSON Object添加空JSON Array并维持字段顺序

JSON字段顺序不符合预期的解决方法

问题描述

我想要生成如下结构的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");

实际输出的JSON:

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

我怀疑是LinkedHashMap的问题,试过设置access order也没用,求解决办法。

解决建议

  • 初始化JSONObject时使用LinkedHashMap
    如果你用的是org.json库,默认JSONObject内部是无序的哈希表。要让顶层字段保持插入顺序,创建JSONObject时直接传入LinkedHashMap作为初始容器:

    // 用LinkedHashMap初始化,保证字段按put顺序排列
    JSONObject obj1 = new JSONObject(new LinkedHashMap<>());
    obj1.put("pricing", pricing);
    obj1.put("description", "test");
    obj1.put("guaranteedDeliveryTime", "testTime");
    obj1.put("offerType", "Currency"); // 注意修正字段名:offerTime -> offerType
    
  • 修正pricing内部的字段顺序
    你的pricing已经用了LinkedHashMap,但输出中discounts跑到pricePerUnit前面,可尝试将discounts的put操作放在最后,同时确保没有其他代码修改了Map的顺序。另外记得把quantity和minQuantity的值修正为目标的4200和10。

  • 换用支持顺序的JSON库
    如果org.json的限制难以满足需求,推荐使用Jackson或Gson:

    • Jackson:定义实体类后,默认按字段声明顺序序列化,也可以用@JsonPropertyOrder指定自定义顺序:
      class Offer {
          private Pricing pricing;
          private String description;
          private String guaranteedDeliveryTime;
          private String offerType;
      
          // 生成getter、setter方法
      }
      
      class Pricing {
          private int quantity;
          private int minQuantity;
          private PricePerUnit pricePerUnit;
          private List<Object> discounts;
      
          // 生成getter、setter方法
      }
      
      class PricePerUnit {
          private double amount;
          private String currency;
      
          // 生成getter、setter方法
      }
      
      // 序列化输出格式化后的JSON
      ObjectMapper mapper = new ObjectMapper();
      String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(yourOfferObject);
      
    • Gson:默认按字段声明顺序序列化,也可以通过注解自定义顺序,使用起来更轻量化。
  • 修正字段名错误
    代码中最后put的offerTime和目标JSON的offerType不符,记得修改字段名,保证输出结构完全匹配。

内容的提问来源于stack exchange,提问作者matthewoak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 03:07:03