JsonReader使用自定义对象模型

时间:2013-07-02 12:57:30

标签: android json api model gson

我正在使用Gson使用自定义模型解析一个巨大的Json Reply。我的问题是Json的回复没有直接启动我的模型。特别是在Json回复的“_data”部分。

MyRequester {
@SerializedName("REQUEST")
private String request;

@SerializedName("RESPONSE")
private Response response;

public String getRequest() {
    return this.request;
}

public void setRequest(String request) {
    this.request = request;
}

public Response getResponse() {
    return this.response;
}

public void setResponse(Response response) {
    this.response = response;
}

public static class Response {
    @SerializedName("_status")
    private int status;

    @SerializedName("_msg")
    private String message;

    @SerializedName("_data")
    private List<InfoModel> data;

    public int getStatus() {
        return this.status;
    }

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

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<InfoModel> getData() {
        return this.data;
    }

    public void setData(List<InfoModel> data) {
        this.data = data;
    }
}
}

InfoModel {
    @SerializedName("id")
    private String id;

@SerializedName("info")
    private MyCustomModel info;

public String getId() {
    return this.id;
}

public void setId(String id) {
    this.id = id;
}

public MyCustomModel getInfo() {
    return this.info;
}

public void setInfo(MyCustomModel info) {
    this.info = info;
}
}


MyCustomModel {
    @SerializedName("no")
    private String no;

    @SerializedName("assigned_user_id")
    private String assigned_user_id;

}

上述模型用于以下代码:

public List<InfoModel> readRPSJsonStream(InputStream in)
        throws IOException {
    Gson gson = new Gson();
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    List<InfoModel> listRPS = new ArrayList<InfoModel>();

    reader.beginObject();
    reader.nextName();
    reader.nextString();
    reader.nextName();
    reader.beginObject();
    reader.nextName();
    reader.nextString();
    reader.nextName();
    reader.nextString();
    reader.nextName();
    reader.beginArray();

    while (reader.hasNext()) {
        InfoModel rps = gson.fromJson(reader, InfoModel .class);
        listRPS.add(rps);
    }
    reader.endArray();
    reader.close();
    return listRPS;
}

我想用更有效的方式替换这些行来直接获取“_data”部分。

reader.beginObject();
    reader.nextName();
    reader.nextString();
    reader.nextName();
    reader.beginObject();
    reader.nextName();
    reader.nextString();
    reader.nextName();
    reader.nextString();
    reader.nextName();
    reader.beginArray();

我想使用MyRequester类,但我不知道如何在StreamReader中使用它。我以前使用以下代码有不同的方法:

Gson gson = new Gson();
    MyRequester myRequester = null;

String URL = "ws/test.php";
    HttpPost httpPost = new HttpPost(URL);

HttpResponse response = UnilabSfaApp.httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String entityString = EntityUtils.toString(entity);
        myRequester = gson.fromJson(entityString,
                    MyRequester.class);
List<InfoModel> listInfo = myRequester.getResponse().getData();

但这不适用于我目前的做法。

Json回复格式为:

{"REQUEST":"Retrieve Multiple Record Details",
  "RESPONSE":
     {"_status":1,"_msg":"SUCCESS: 2 Record(s) found.","_data":
     [{"id":"43x7638",
       "info":
          {
          "no":"RPS739",
          "assigned_user_id":"19x1",
          }
      },

      {"id":"43x7654",
          "info":
          {
          "no":"RPS755",
          "assigned_user_id":"19x1",
          }
      }
    ]
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解了您的问题...但是,为什么不直接使用您的班级MyRequester来反复排序?就像这样:

JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
MyRequester myRequester = gson.fromJson(reader, MyRequester.class);