2

How do I to convert a JSON string to object in Java Android so that I can access "name2"

thanks

// this is how my json looks {"name1":"a","name2":"b","name3":"c","name4":"d"}

String mytext = GetMyJSON();

JSONObject obj = new JSONObject(mytext);

Strin N1  = obj.name1;
1
  • @nr4bt sugersts the legal way to map your json to object. Commented Jan 25, 2014 at 12:27

2 Answers 2

7

If a library is ok for you, GSON would help about it. it converts JSON to object, so you can reach them such as yourclassinst.name

refer this one

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

1 Comment

I'd recommend Jackson over GSON, it's faster. wiki.fasterxml.com/JacksonDownload
4

Do Something like this

JSONObject obj = new JSONObject(mytext);

if(obj!=null)
{
  Strin N1  = obj.getString("name1");
  Strin N2  = obj.getString("name2");
  Strin N3  = obj.getString("name3");
  Strin N4  = obj.getString("name4");
}

1 Comment

And where is the object to which is converted the JSON?

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.