Android的JSON解析与GSON

Android的JSON解析与GSON

本文介绍了Android的JSON解析与GSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我特林使用映射从GSON一个URL到我的java类JSON数据。这是code作为类:

 公共类会话{
公共布尔活跃;
公共字符串contributor_covu_id;
公共字符串created_at;
公共字符串键;
公共字符串状态;公共布尔getActive(){
    返回活跃;
}
公共无效SETACTIVE(布尔有效){
    this.active =有效;
}
公共字符串getContributor_covu_id(){
    返回contributor_covu_id;
}
公共无效setContributor_covu_id(字符串contributor_covu_id){
    this.contributor_covu_id = contributor_covu_id;
}
公共字符串getCreated_at(){
    返回created_at;
}
公共无效setCreated_at(字符串created_at){
    this.created_at = created_at;
}
公共字符串getKey(){
    返回键;
}
公共无效的setkey(字符串键){
    this.key =键;
}
公共字符串的getStatus(){
    返回状态;
}
公共无效setStatus(字符串状态){
    this.status =状态;
}
公共字符串的getType(){
    返回类型;
}
公共无效的setType(字符串类型){
    this.type =类型;
}
公共字符串类型;

}

这是调用服务和地图JSON之类的code

 公共静态列表<会议> getSessions(字符串urlString)
        抛出IOException    会话[]会议;
    清单<会议> tempList = NULL;
    网址URL =新的URL(urlString);
    HttpURLConnection的康恩=(HttpURLConnection类)url.openConnection();
    GsonBuilder gsonb =新GsonBuilder();
    GSON GSON = gsonb.create();
    StringBuilder的SB =新的StringBuilder();
    串线;    //响应响应= NULL;
    JSONObject的J = NULL;    如果(conn.getResponse code()!= 200){
        抛出新IOException异常(conn.getResponseMessage());
    }    的BufferedReader的BufferedReader =新的BufferedReader(
            新的InputStreamReader(conn.getInputStream()));    而((行= bufferedReader.readLine())!= NULL){
        sb.append(线);
    }
    bufferedReader.close();
    conn.disconnect();    尝试{
        J =新的JSONObject(sb.toString());
        会议= gson.fromJson(j.toString(),会话[]类);
        tempList = Arrays.asList(会话);        //响应= gson.fromJson(j.toString(),Response.class);
        // tempList.add(响应);    }赶上(JSONException E){
        e.printStackTrace();
    }
    返回tempList;
}

虽然code是正确的,我得到一个GSON异常无法调用类com.test.Sessions无参数的构造函数;.注册与GSON的InstanceCreator这种类型可能会解决这个问题。

我要如何解决这个问题,并返回GSON作为一个ArrayList

解决方案

解决的办法是:

 公共类会话{私人字符串状态;
私人列表<会议>会议;公共字符串的getStatus(){
    返回状态;
}公共无效setStatus(字符串状态){
    this.status =状态;
}公开名单<会议> getSessions(){
    返回会议;
}公共无效setSessions(列表<会议>会话){
    this.sessions =会议;
}公共静态类会话{
    公共布尔活跃;
    公共字符串contributor_covu_id;
    公共字符串created_at;
    公共字符串键;
    公共字符串状态;
    公共字符串名称;};

}

I am tring to map json data using Gson from a URL to my java class. This is the code for the class:

public class Sessions {
public Boolean active;
public String contributor_covu_id;
public String created_at;
public String key;
public String status;

public Boolean getActive() {
    return active;
}
public void setActive(Boolean active) {
    this.active = active;
}
public String getContributor_covu_id() {
    return contributor_covu_id;
}
public void setContributor_covu_id(String contributor_covu_id) {
    this.contributor_covu_id = contributor_covu_id;
}
public String getCreated_at() {
    return created_at;
}
public void setCreated_at(String created_at) {
    this.created_at = created_at;
}
public String getKey() {
    return key;
}
public void setKey(String key) {
    this.key = key;
}
public String getStatus() {
    return status;
}
public void setStatus(String status) {
    this.status = status;
}
public String getType() {
    return type;
}
public void setType(String type) {
    this.type = type;
}
public String type;

}

and this is the code of the class that calls the service and maps json

public static List<Sessions> getSessions(String urlString)
        throws IOException {

    Sessions[] sessions;
    List<Sessions> tempList = null;
    URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    GsonBuilder gsonb = new GsonBuilder();
    Gson gson = gsonb.create();
    StringBuilder sb = new StringBuilder();
    String line;

    // Response response = null;
    JSONObject j = null;

    if (conn.getResponseCode() != 200) {
        throw new IOException(conn.getResponseMessage());
    }

    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));

    while ((line = bufferedReader.readLine()) != null) {
        sb.append(line);
    }
    bufferedReader.close();
    conn.disconnect();

    try {
        j = new JSONObject(sb.toString());
        sessions = gson.fromJson(j.toString(), Sessions[].class);
        tempList = Arrays.asList(sessions);

        // response = gson.fromJson(j.toString(), Response.class);
        // tempList.add(response);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return tempList;
}

Although the code is correct, I get a Gson exception "Unable to invoke no-args constructor for class com.test.Sessions;. Register an InstanceCreator with Gson for this type may fix this problem"

How do i solve this issue and return the Gson as an ArrayList?

解决方案

The solution is:

public class Sessions {

private String status;
private List<Session> sessions;

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public List<Session> getSessions() {
    return sessions;
}

public void setSessions(List<Session> sessions) {
    this.sessions = sessions;
}

public static class Session {
    public Boolean active;
    public String contributor_covu_id;
    public String created_at;
    public String key;
    public String status;
    public String name;

};

}

这篇关于Android的JSON解析与GSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 11:40