如何使用Java中的Gson库将对象列表自动转换为JSONArray?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22118984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:44:29  来源:igfitidea点击:

How to convert automatically list of objects to JSONArray with Gson library in java?

javajsongson

提问by snaggs

Lets say I have following list of custom object: ArrayList<GroupItem>where GroupItemis some class that has intand Stringvariables.

可以说我有以下的自定义对象的名单:ArrayList<GroupItem>这里GroupItem是一些具有类intString变量。

I tried so far Gsonlibrary but it's not exactly what I want.

到目前为止,我尝试过Gson图书馆,但这不是我想要的。

ArrayList<GroupItem> groupsList = /* ... */
Gson gson = new Gson();
JsonElement element = gson.toJsonTree(groupsList, new TypeToken<ArrayList<GroupItem>>() {}.getType());

JsonArray jsonArray = element.getAsJsonArray();

My goal is to get JSONArray(org.Json.JSONArray) somehow. Any ideas?

我的目标是以JSONArray某种方式获得(org.Json.JSONArray)。有任何想法吗?

[EDIT]

[编辑]

My project is Android with Cordova that has API based on org.Json.JSONArray

我的项目是带有 Cordova 的 Android,它具有基于 org.Json.JSONArray

ANd I thought to write some generic way to convert instances to JSONArray/ JSONObject

而且我想写一些通用的方法来将实例转换为JSONArray/JSONObject

Thanks,

谢谢,

采纳答案by snaggs

This way is worked for me:

这种方式对我有用:

Convert all list to String.

将所有列表转换为字符串。

String element = gson.toJson(
                    groupsList,
           new TypeToken<ArrayList<GroupItem>>() {}.getType());

Create JSONArrayfrom String:

JSONArray从字符串创建:

    JSONArray list = new JSONArray(element);

回答by jeremyjjbrown

Why do you want to use GSON to convert to an org.json class? Nor do you need to write any custom code to get a JSONArray. The org.json apiprovides a way to do it.

为什么要使用 GSON 转换为 org.json 类?您也不需要编写任何自定义代码来获取 JSONArray。该org.json API提供一种方式来做到这一点。

  LinkedList list = new LinkedList();
  list.add("foo");
  list.add(new Integer(100));
  list.add(new Double(1000.21));
  list.add(new Boolean(true));
  list.add(null);
  JSONArray jsonArray = new JSONArray(list);
  System.out.print(jsonArray);

回答by weston

You don't need gson. There's a constructorthat takes a collection, so just:

你不需要gson。有一个接受集合的构造函数,所以只需:

ArrayList<GroupItem> groupsList =... ;
JSONArray JSONArray = new JSONArray(groupsList);

回答by Brian Roach

The org.jsonbased classes included in Android don't have any features related to converting Java POJOs to/from JSON.

org.jsonAndroid 中包含的基础类没有任何与将 Java POJO 转换为 JSON 或从 JSON 转换相关的功能。

If you have a list of some class (List<GroupItem>) and you absolutely need to convert that to a org.json.JSONArrayyou have two choices:

如果您有某个类 ( List<GroupItem>)的列表并且您绝对需要将其转换为 aorg.json.JSONArray您有两个选择:

A) Use Gson or Hymanson to convert to JSON, then parse that JSON into a JSONArray:

A) 使用 Gson 或 Hymanson 转换为 JSON,然后将该 JSON 解析为JSONArray

List<GroupItem> list = ...
String json = new Gson().toJson(list);
JSONArray array = new JSONArray(json);

B) Write code to create the JSONArrayand the JSONObjects it will contain from your Java objects:

B) 编写代码以从您的 Java 对象创建JSONArrayJSONObject它将包含的s:

JSONArray array = new JSONArray();
for (GroupItem gi : list)
{
    JSONObject obj = new JSONObject();
    obj.put("fieldName", gi.fieldName);
    obj.put("fieldName2", gi.fieldName2);
    array.put(obj);
}

回答by Ronald

Use Hymanson for JSON to Object or Object to JSON

使用 Hymanson for JSON to Object 或 Object to JSON

See the next link Hymanson examples

请参阅下一个链接Hyman逊示例

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
StringWriter stringWriter = new StringWriter();
objectMapper.writeValue(stringWriter, groupsList );
System.out.println("groupsList JSON is\n"+stringWrite);