【问题标题】:How to put array into Hashmap for encoding a JSON object如何将数组放入 Hashmap 以编码 JSON 对象
【发布时间】:2017-05-13 05:45:30
【问题描述】:

我正在尝试使用 json-simple 1.1.1 发送 API 调用,并将字段和值保存为 HashMap。我应该发送这些参数:

{ api_key : string,
  product_id : string,
  name : string,
  tax_rates : array }

这是一个 HashMap 示例:

HashMap<String,Object> arg = new HashMap<String, Object>();
            arg.put("product_id","42");
            arg.put("name", "EKOS");
            arg.put("tax_rates", taxarray);

我也将 taxarray 保存为 HashMap:

HashMap<String, Object> taxarray = new HashMap<String, Object>();
            taxarray.put("name","EKOS");
            taxarray.put("type", "type_value_fixed");
            taxarray.put("value", "56");

但是当我执行 API 调用时,它会返回错误:参数“tax_rates”无效。所需的参数类型是数组。

我一直在尝试将 taxarray HashMap 也保存为 JSONArray。你能帮我解决这个问题吗?

另一个问题:如何在一个“tax_rates”中保存 2 个或多个税率?这是一个例子:

 HashMap<String,Object> arg = new HashMap<String, Object>();
                arg.put("product_id","42");
                arg.put("name", "EKOS");
                arg.put("tax_rates", array [
                                     taxarray1[],
                                     taxarray2[]
                                           ]);

【问题讨论】:

  • 建议您应该使用 Json 对象来存储对象,例如:{ "product_id": 42, "name": "EKOS", "tax_rates": { "a": "b", "c": "d", "e": "f" } }
  • 嗯...我不推荐这种方法。 HashMap 不等同于 JavaScript 对象;它的目的是捕获实现相同接口的对象。虽然你可以让它工作,但这不是 Java 的方式。我建议尝试类似mkyong.com/java/jackson-2-convert-java-object-to-from-json 的方法,以使用现有工具将您的 JSON 映射到 Java 类,从而以更 Java 的方式捕获您的预期结构。

标签: java arrays json serialization hashmap


【解决方案1】:

你应该有这样的东西 - 税类:

public class Tax {
    String name;
    String type;
    Integer[] values;

    public Tax(String name, String type, Integer[] values) {
        this.name = name;
        this.type = type;
        this.values = values;
    }   
}

然后对tax_rates : array使用Tax类的对象数组而不是HashMap。

这段代码使用google json:

Map<String, Object> arg = new HashMap<String, Object>();
arg.put("product_id", "42");
arg.put("name", "EKOS");
arg.put("tax_rates",
                new Tax[] { new Tax("EKOS", "type_value_fixed", new Integer[] { 1, 2, 3 }),
                        new Tax("ABC", "type_value_fixed", new Integer[] { 4, 5 }),
                        new Tax("DEF", "type_value_fixed", new Integer[] { 6, 7}) });

Gson gson = new Gson();

System.out.println(gson.toJson(arg));

会给你这样的json:

{
  "product_id": "42",
  "name": "EKOS",
  "tax_rates": [
    {
      "name": "EKOS",
      "type": "type_value_fixed",
      "values": [
        1,
        2,
        3
      ]
    },
    {
      "name": "ABC",
      "type": "type_value_fixed",
      "values": [
        4,
        5
      ]
    },
    {
      "name": "DEF",
      "type": "type_value_fixed",
      "values": [
        6,
        7
      ]
    }
  ]
}

【讨论】:

  • 我推荐这个想法;甚至对于他的项目架构来说也会更好。
【解决方案2】:

tax_rates 必须是一个数组,所以这样做:

List<Double> taxRates = new ArrayList<Double>();
taxRates.add(19);
taxRates.add(17.5);

Map<String,Object> arg = new HashMap<String, Object>();
arg.put("product_id","42");
arg.put("name", "EKOS");
arg.put("tax_rates", taxRates);

【讨论】:

    猜你喜欢
    • 2021-08-22
    • 2021-07-18
    • 2020-10-20
    • 1970-01-01
    • 2017-07-27
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多