您的对象必须保留 json 指令的层次结构。对于您的示例,它将是这样的:
public class Object {
@SerializedName("field1")
public String fieldOne;
@SerializedName("field2")
public String fieldTwo;
@SerializedName("field3")
public String fieldThree;
@SerializedName("field4")
public String fieldFour;
}
public class Name {
@SerializedName("object1")
public Object obj;
}
public class GsonObj {
@SerializedName("Name")
public Name name;
}
使用以下调用:
String json = "{\"Name\":{" +
"\"object1\":{" +
"\"field1\":\"17\",\"field2\":\"360\",\"field3\":\"19\",\"field4\":\"sun\",\"field5\":\"rain\"}}}";
Gson gson = new Gson();
GsonObj jsonResult = gson.fromJson(json, GsonObj.class);
Log.d("test", "field one: "+jsonResult.name.obj.fieldOne);
Log.d("test", "field two: "+jsonResult.name.obj.fieldTwo);
Log.d("test", "field three: "+jsonResult.name.obj.fieldThree);
Log.d("test", "field four: "+jsonResult.name.obj.fieldFour);