Getting started with Android (XXII) parsing JSON

Original link: http://www.orlion.ga/687/

There are many ways to parse JSON, mainly including officially provided jsonobject and Google's open source library gson. In addition, some third-party open source libraries such as Jackson and fastjson are also very good.

Assume that the JSON data is:

[{"id":"5","version":"5.5","name":"Angry Birds"},{"id":"6","version":"7.0","name":"Clash of Clans"},{"id":"7","version":"3.5","name":"Hey Day"}]

1、 Jsonobject

try {
    JSONArray jsonArray = new JSONArray(jsonData);
    for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    String id = jsonObject.getString("id");
    String name = jsonObject.getString("name");
    String version = jsonObject.getString("version");
    Log.d("MainActivity", "id is " + id);
    Log.d("MainActivity", "name is " + name);
    Log.d("MainActivity", "version is " + version);
}
} catch (Exception e) {
    e.printStackTrace();
}

First, the data returned by the server is passed into a jsonarray object. Then loop through the jsonarray. Each element extracted from it is a jsonobject object, and each jsonobject object will contain data such as ID, name and version. Next, you just need to call the getString () method to take out the data and print it out.

2、 Using gson

It is mainly used to automatically map a JSON format string into an object. For example, a JSON format data is as follows:

{"name":"Tom","age":20}

Then we can define a person class, add the name and age fields, and then simply call the following code to automatically parse JSON data into a person object:

Gson gson = new Gson();
Person person = gson.fromJson(jsonData, Person.class);

If a JSON array needs to be parsed, it will be a little troublesome. We need to pass the data type expected to be parsed into the fromjson () method with the help of typetoken, as shown below:

List<Person> people = gson.fromJson(jsonData, new TypeToken<List<Person>>()
{}.getType());

For the JSON data at the beginning of this article, test gson. First, create the class app:

public class App {
    private String id;
    private String name;
    private String version;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getVersion() {
        return version;
    }
    public void setVersion(String version) {
        this.version = version;
    }
}

Resolution:

Gson gson = new Gson();
List<App> appList = gson.fromJson(jsonData, new
TypeToken<List<App>>() {}.getType());
for (App app : appList) {
    Log.d("MainActivity", "id is " + app.getId());
    Log.d("MainActivity", "name is " + app.getName());
    Log.d("MainActivity", "version is " + app.getVersion());
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>