GSON将数据对象的数组到JSON - 机器人 [英] GSON turn an array of data objects into json - Android

查看:205
本文介绍了GSON将数据对象的数组到JSON - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在与web视图前端的原生Android应用程序。

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

我是这样的:

public class dataObject
{
  int a;
  String b;
}

和在活动中,

我已经做数据对象的数组,表示数据对象X [5];

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

现在我想在回调函数通过这5个数据对象,以我的JavaScript web视图界面JSON。

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

我看了看,通过互联网,似乎最喜欢的教程来谈谈如何转换 fromJson()。有没有很多关于的toJSON()。我发现一个告诉我, dataObject.toJson(),是可行的。

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.

但我怎么能够通过全部5个数据对象?

But how can I pass all 5 dataObjects?

我是pretty的新本。谢谢

I am pretty new to this. Thanks

推荐答案

下面是关于如何使用GSON使用对象列表的COM prehensive例子。这应该表现出的完全的如何转换到/从Json的,如何引用列表等。

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.

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,你必须切换; 在previous两个命令。

Note that if you're using Windows, you'll have to switch : 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

请记住,这仅仅是一个命令行程序的演示的它是如何工作的,但同样的原则也适用Android的环境(参考JAR库等)。在

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.)

这篇关于GSON将数据对象的数组到JSON - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆