【问题标题】:Gson turn an array of data objects into json - AndroidGson 将一组数据对象转换为 json - Android
【发布时间】:2012-02-29 12:05:18
【问题描述】:

目前我正在开发一个带有 webView 前端的原生 android 应用程序。

我有类似的东西:

public class dataObject
{
  int a;
  String b;
}

在活动中,

我已经创建了一个 dataObject 数组,比如 dataObject x[5];

现在我想在回调函数中将这 5 个数据对象作为 JSON 传递给我的 javascript webView 接口。

我浏览了互联网,似乎大多数教程都在谈论如何转换fromJson()。关于toJson() 的内容不多。我找到了一个告诉我dataObject.toJson() 会起作用的人。

但是我怎样才能传递所有 5 个数据对象呢?

【问题讨论】:

  • 试试String json = new Gson().toJson(dataObjects) 其中dataObjects是一个dataObject[]

标签: java android json gson


【解决方案1】:

这是一个关于如何将 Gson 与对象列表一起使用的综合示例。这应该准确地演示如何与 Json 相互转换、如何引用列表等。

Test.java

import com.google.gson.Gson;
import java.util.List;
import java.util.ArrayList;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;


public class Test {

  public static void main (String[] args) {

    // Initialize a list of type DataObject
    List<DataObject> objList = new ArrayList<DataObject>();
    objList.add(new DataObject(0, "zero"));
    objList.add(new DataObject(1, "one"));
    objList.add(new DataObject(2, "two"));

    // Convert the object to a JSON string
    String json = new Gson().toJson(objList);
    System.out.println(json);

    // Now convert the JSON string back to your java object
    Type type = new TypeToken<List<DataObject>>(){}.getType();
    List<DataObject> inpList = new Gson().fromJson(json, type);
    for (int i=0;i<inpList.size();i++) {
      DataObject x = inpList.get(i);
      System.out.println(x);
    }

  }


  private static class DataObject {
    private int a;
    private String b;

    public DataObject(int a, String b) {
      this.a = a;
      this.b = b;
    }

    public String toString() {
      return "a = " +a+ ", b = " +b;
    }
  }

}

编译它:

javac -cp "gson-2.1.jar:." Test.java

最后运行它:

java -cp "gson-2.1.jar:." Test

请注意,如果您使用的是 Windows,则必须在前两个命令中将 :; 切换。

运行之后,应该会看到如下输出:

[{"a":0,"b":"zero"},{"a":1,"b":"one"},{"a":2,"b":"two"}]
a = 0, b = zero
a = 1, b = one
a = 2, b = two

请记住,这只是一个演示其工作原理的命令行程序,但同样的原则也适用于 Android 环境(引用 jar 库等)

【讨论】:

  • DataObject[] 与 List 的行为是否相同?
  • 回答我自己的问题:是的,行为是一样的。
  • 不要使用 ArrayList a = new ArrayList(); Gson.toJson(a) 应该使用 List a = new ArrayList(); Gson.toJson(a)
【解决方案2】:

我使用辅助类的 gson 列表反序列化版本:

public List<E> getList(Class<E> type, JSONArray json) throws Exception {
    Gson gsonB = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

    return gsonB.fromJson(json.toString(), new JsonListHelper<E>(type));
}



public class JsonListHelper<T> implements ParameterizedType {

  private Class<?> wrapped;

  public JsonListHelper(Class<T> wrapped) {
    this.wrapped = wrapped;
  }

  public Type[] getActualTypeArguments() {
    return new Type[] {wrapped};
  }

  public Type getRawType() {
    return List.class;
  }

  public Type getOwnerType() {
    return null;
  }

}

用法

List<Object> objects = getList(Object.class, myJsonArray);

【讨论】:

    猜你喜欢
    • 2016-08-07
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    相关资源
    最近更新 更多