Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert JSON string to object in JAVA android

Tags:

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;
like image 294
Hello-World Avatar asked May 31 '26 09:05

Hello-World


2 Answers

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

like image 61
Orhan Obut Avatar answered Jun 02 '26 22:06

Orhan Obut


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");
}
like image 21
Chirag Ghori Avatar answered Jun 02 '26 21:06

Chirag Ghori