How do I extract value from Json

前端 未结 8 1126
野的像风
野的像风 2020-11-30 05:23

I am getting a response String from server like below

{
  \"name\": \"Json\",
  \"detail\": {
    \"first_name\": \"Json\",
    \"last_name\": \"Scott\",
            


        
8条回答
  •  野性不改
    2020-11-30 05:39

    Pasting my code here, this should help. It shows the package which can be used.

    import org.json.JSONException;
    import org.json.JSONObject;
    
    public class extractingJSON {
    
        public static void main(String[] args) throws JSONException {
            // TODO Auto-generated method stub
    
            String jsonStr = "{\"name\":\"SK\",\"arr\":{\"a\":\"1\",\"b\":\"2\"}}";
            JSONObject jsonObj = new JSONObject(jsonStr);
            String name = jsonObj.getString("name");
            System.out.println(name);
    
            String first = jsonObj.getJSONObject("arr").getString("a");
            System.out.println(first);
    
        }
    
    }
    

提交回复
热议问题