[英]How to create a class to get the json response of a service which returns in an structure like the following?
Web 服務返回以下 JSON 字符串:
{"errorCode":0,"error":"","status":"OK","data":{"id":"1234A"}}
因此,要獲得一個在 function 中接收響應的 class,就像在 Retrofit 中執行一個帖子一樣:
Call<UploadImageData> postData(@Header("Cookie") String sessionId, @Body UploadImageModal image);
我需要像這樣制作一個 class ;
public class UploadImageData {
private int errorCode;
private String error;
private String status;
}
但是我迷失了如何聲明將采用“數據”的部分:{“id”:“1234A”},因此它可以正確地從那里獲取數據。
我怎么能這樣做?
Since data is a nested object within the surrounding json object, you can include it as another class in your UploadImageData class.
public class UploadImageData {
private int errorCode;
private String error;
private String status;
private MyDataClass data;
}
public class MyDataClass {
private String id;
}
不要忘記設置方法或公開字段。
導入 net.sf.json.JSONObject;
字符串 json ="{"errorCode":0,"error":"","status":"OK","data":{"id":"1234A","id":"14A"}} ";
private static void resolveJson(String json) {
JSONObject jsonObj = JSONObject.fromObject(json);
System.out.println(jsonObj.get("errorCode"));
System.out.println(jsonObj.get("error"));
System.out.println(jsonObj.get("status"));
JSONObject data = jsonObj.getJSONObject("data");
System.out.println(data.get("id"));
}
//輸出
0
好的 ["1234A","14A"]
使用如上方法解析Json
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.