简体   繁体   中英

Deserializing Json to java classes using gson library

在此处输入图片说明 I have these two classes:

public class Root {
    public ProfileInfoResult ProfileInfoResult = new ProfileInfoResult();
}

public class ProfileInfoResult {
    public String joiningYear;
}

this is my json string which i have to deserialize:

{"ProfileInfoResult":{"joiningYear":"2009"}}

This is code to deserialize:

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 debug says that resultObj cannotbe resolved to a variable.

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);

this is working fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM