1

I have a JSON string in the format

{"id":100,"nickname":"yash","name":"Rahul"}

which i got with the code below:

import java.io.IOException;
import java.io.StringWriter;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

    public class Json {
        public static void main(String str[]) throws IOException
        {

          JSONObject obj=new JSONObject();
          obj.put("name","Rahul");
          obj.put("id",100);
          obj.put("nickname","yash");
          StringWriter out = new StringWriter();
          obj.writeJSONString(out);
          String jsonText = out.toString();
          System.out.println(jsonText);
            }
    }

Now, how do i parse this JSON string and get the values??

yAsH
  • 3,367
  • 8
  • 36
  • 67
  • 2
    Seriously, use google and get 10 billion results.. – Rob Mar 27 '13 at 10:37
  • Frankly: both of the linked-to questions have somewhat sub-par answers (mostly consisting of links only). If someone finds a duplicate with *good* answers, please tell us. – Joachim Sauer Mar 27 '13 at 10:43

1 Answers1

2

Use JSONObject.get() method Like this:

    String name = (String) obj.get("name");
    int ID = Integer.parseInt((String) obj.get("id"));
    String nickname = (String) obj.get("nickname");


    System.out.println("Name=  " + name);
    System.out.println("ID=  " + ID );
    System.out.println("nickname=" + nickname);
Alya'a Gamal
  • 5,624
  • 19
  • 34