Gson将数据对象数组转换为json - Android

时间:2022-02-19 16:46:53

Currently I am working on a native android app with webView front end.

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

I have something like:

我有类似:

public class dataObject
{
  int a;
  String b;
}

and in activity,

在活动,

I have made an array of dataObject, say dataObject x[5];

我做了一个dataObject的数组,比如dataObject x[5];

Now i want to pass these 5 dataObject to my javascript webView interface as JSON in a callback function.

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

I looked through the internet, seems like most tutorials talk about how to convert fromJson(). There are not a lot about toJson(). I found one that taught me that dataObject.toJson(), would work.

我浏览了一下internet,似乎大多数教程都讨论如何从json()转换。有关toJson()的内容不多。我找到了一个可以教会我dataobjecter . tojson()的方法。

But how can I pass all 5 dataObjects?

但是如何传递所有5个数据对象呢?

2 个解决方案

#1


82  

Here's a comprehensive example on how to use Gson with a list of objects. This should demonstrate exactly how to convert to/from Json, how to reference lists, etc.

这里有一个关于如何在对象列表中使用Gson的全面示例。这应该演示如何从Json转换成/从Json,如何引用列表等等。

Test.java:

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;
    }
  }

}

To compile it:

编译:

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

And finally to run it:

最后运行它:

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

Note that if you're using Windows, you'll have to switch : with ; in the previous two commands.

注意,如果你使用Windows,你必须切换:with;在前两个命令中。

After you run it, you should see the following output:

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

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

Keep in mind that this is only a command line program to demonstrate how it works, but the same principles apply within the Android environment (referencing jar libs, etc.)

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

#2


0  

My version of gson list deserialization using a helper class:

我的版本的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;
  }

}

Usage

使用

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

#1


82  

Here's a comprehensive example on how to use Gson with a list of objects. This should demonstrate exactly how to convert to/from Json, how to reference lists, etc.

这里有一个关于如何在对象列表中使用Gson的全面示例。这应该演示如何从Json转换成/从Json,如何引用列表等等。

Test.java:

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;
    }
  }

}

To compile it:

编译:

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

And finally to run it:

最后运行它:

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

Note that if you're using Windows, you'll have to switch : with ; in the previous two commands.

注意,如果你使用Windows,你必须切换:with;在前两个命令中。

After you run it, you should see the following output:

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

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

Keep in mind that this is only a command line program to demonstrate how it works, but the same principles apply within the Android environment (referencing jar libs, etc.)

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

#2


0  

My version of gson list deserialization using a helper class:

我的版本的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;
  }

}

Usage

使用

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