[英]Gson turn an array of data objects into json - Android
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()
. 我浏览了一下互联网,似乎大多数教程都谈到如何转换fromJson()
。 There are not a lot about toJson()
. 关于toJson()
并没有很多。 I found one that taught me that dataObject.toJson()
, would work. 我找到了一个告诉我dataObject.toJson()
工作。
But how can I pass all 5 dataObjects? 但是如何通过所有5个dataObjects?
Here's a comprehensive example on how to use Gson with a list of objects. 这是一个关于如何将Gson与对象列表一起使用的综合示例。 This should demonstrate exactly how to convert to/from Json, how to reference lists, etc. 这应该准确地演示如何转换到/从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 ;
请注意,如果您使用的是Windows,则必须切换:
with ;
in the previous two commands. 在前两个命令中。
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库等)
My version of gson list deserialization using a helper class: 我的使用帮助程序类的gson list反序列化版本:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.