【问题标题】:How to convert List to a JSON Object using GSON?如何使用 GSON 将 List 转换为 JSON 对象?
【发布时间】:2015-03-09 17:00:52
【问题描述】:

我有一个需要使用 GSON 将其转换为 JSON 对象的列表。我的 JSON 对象中有 JSON 数组。

public class DataResponse {

    private List<ClientResponse> apps;

    // getters and setters

    public static class ClientResponse {
        private double mean;
        private double deviation;
        private int code;
        private String pack;
        private int version;

        // getters and setters
    }
}

下面是我的代码,我需要将我的列表转换为其中包含 JSON 数组的 JSON 对象 -

public void marshal(Object response) {

    List<DataResponse.ClientResponse> clientResponse = ((DataResponse) response).getClientResponse();

    // now how do I convert clientResponse list to JSON Object which has JSON Array in it using GSON?

    // String jsonObject = ??
}

到目前为止,我在 List 中只有两个项目 - 所以我需要这样的 JSON 对象 -

{  
   "apps":[  
      {  
         "mean":1.2,
         "deviation":1.3
         "code":100,
         "pack":"hello",
         "version":1
      },
      {  
         "mean":1.5,
         "deviation":1.1
         "code":200,
         "pack":"world",
         "version":2
      }
   ]
}

最好的方法是什么?

【问题讨论】:

  • 您显示的 JSON 不是 JSON 数组,而是 JSON 对象。
  • 哎呀抱歉,是的,它是 JSON 对象。让我重新表述一下这个问题。

标签: java arrays json gson


【解决方案1】:

google gson documentation 提供了一个关于如何将列表实际转换为 json 字符串的示例:

Type listType = new TypeToken<List<String>>() {}.getType();
 List<String> target = new LinkedList<String>();
 target.add("blah");

 Gson gson = new Gson();
 String json = gson.toJson(target, listType);
 List<String> target2 = gson.fromJson(json, listType);

您需要在toJson方法中设置列表的类型,并传递列表对象将其转换为json字符串,反之亦然。

【讨论】:

  • 这是不必要的,不会产生他们要求的 JSON。
  • 这实际上是我唯一有效的答案。
  • 这不会为 json 创建任何键名。如何按 OP 预期创建键名?
【解决方案2】:

如果您的marshal 方法中的responseDataResponse,那么这就是您应该序列化的内容。

Gson gson = new Gson();
gson.toJson(response);

这将为您提供所需的 JSON 输出。

【讨论】:

    【解决方案3】:

    假设你也想获取格式的 json

    {
      "apps": [
        {
          "mean": 1.2,
          "deviation": 1.3,
          "code": 100,
          "pack": "hello",
          "version": 1
        },
        {
          "mean": 1.5,
          "deviation": 1.1,
          "code": 200,
          "pack": "world",
          "version": 2
        }
      ]
    }
    

    而不是

    {"apps":[{"mean":1.2,"deviation":1.3,"code":100,"pack":"hello","version":1},{"mean":1.5,"deviation":1.1,"code":200,"pack":"world","version":2}]}
    

    你可以使用漂亮的打印。为此,请使用

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(dataResponse);
    

    【讨论】:

      【解决方案4】:

      确保将您的集合转换为数组:

      Gson().toJson(objectsList.toTypedArray(), Array<CustomObject>::class.java)
      

      【讨论】:

        【解决方案5】:

        我们还可以使用另一种解决方法,首先创建一个 myObject 数组,然后将它们转换为列表。

        final Optional<List<MyObject>> sortInput = Optional.ofNullable(jsonArgument)
                        .map(jsonArgument -> GSON.toJson(jsonArgument, ArrayList.class))
                        .map(gson -> GSON.fromJson(gson, MyObject[].class))
                        .map(myObjectArray -> Arrays.asList(myObjectArray));
        

        好处:

        • 我们在这里没有使用反射。 :)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-10-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-07
          • 2015-09-13
          • 2013-11-15
          相关资源
          最近更新 更多