我有这两个类:
public class Root {
public ProfileInfoResult ProfileInfoResult = new ProfileInfoResult();
}
public class ProfileInfoResult {
public String joiningYear;
}
这是我的json字符串,我必须反序列化:
{"ProfileInfoResult":{"joiningYear":"2009"}}
这是反序列化的代码:
Gson gson = new Gson();
JSONObject obj = new JSONObject(responseString); //responseString= {"ProfileInfoResult":{"joiningYear":"2009"}}
Root resultObj = new Root();
String resStr = obj.toString();
try
{
resultObj = gson.fromJson(resStr, Root.class);
}
catch(Exception e)
{
Log.i("myyyy", e.getMessage());
e.printStackTrace();
}
Java调试说resultObj无法解析为变量。
答案 0 :(得分:1)
String str = "{\"ProfileInfoResult\":{\"joiningYear\":\"2009\"}}";
Root root = new Root();
Gson gson = new Gson();
root = gson.fromJson(str, Root.class);
TextView b = new TextView(context);
b.setText(root.ProfileInfoResult.joiningYear);
这很好。