1

enter image description hereI 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.

7
  • Where does the error point to in your code? Commented Jul 3, 2014 at 7:12
  • No error in the code but after executing this line resultObj = gson.fromJson(resStr, Root.class); resultObj cannotbe resolved to a variable in variables of debug section. Commented Jul 3, 2014 at 7:13
  • This code looks very reasonable, and based on your message your error probably doesn't have to do with JSON or Gson. Can you post the whole class, or confirm that the error happens within a line you posted? Commented Jul 3, 2014 at 7:14
  • What is this debug section? Are you using an IDE? Can you post a picture? Commented Jul 3, 2014 at 7:14
  • yes i have added the image now. I'm using eclipse. Commented Jul 3, 2014 at 7:21

1 Answer 1

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

this is working fine.

Sign up to request clarification or add additional context in comments.

5 Comments

is that due to escape sequences missing in my string?
from where you are getting your string, from any service or it is just hard coded?
then escape sequences not an issue.
I don't know why but it automatically start working.
there was some error warning on this line Root resultObj = new Root();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.