Hello-World
Hello-World

Reputation: 9555

how to convert JSON string to object in JAVA android

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;

Upvotes: 2

Views: 3691

Answers (2)

Nathua
Nathua

Reputation: 8836

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

Upvotes: 7

Chirag Ghori
Chirag Ghori

Reputation: 4231

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

Upvotes: 4

Related Questions