请我有一个这样的json响应字符串:
{"result":{"id":21456,"name":"3mm nail","type":"2" }}
这是我的代码:
class rootObj{
List<Result> result;
}
public class Result {
@SerializedName("id")
public String idItem;
@SerializedName("name")
public String name;
}
public static void main(String[] args) throws Exception {
Gson gson = new Gson();
Result result = gson.fromJson(json,Result.class);
System.out.println(result.name);
}
但结果是空的 :( 提前谢谢。
所以..这段代码是我的目标:
class ResultData{
private Result result;
public class Result {
private String id;
private String name;
}
}
...
Gson gson = new Gson();
ResultData resultData = new Gson().fromJson(json, ResultData.class);
System.out.println(resultData.result.id);
System.out.println(resultData.result.name);
感谢 BalusC 给了我这个想法。 Java - 嵌套在嵌套中的 Gson 解析