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;
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Hello-World
  • 9,277
  • 23
  • 88
  • 154

2 Answers2

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

Orhan Obut
  • 8,756
  • 5
  • 32
  • 42
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");
}
Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35