How to convert jsonString to JSONObject in Java

前端 未结 19 3038
一生所求
一生所求 2020-11-22 00:39

I have String variable called jsonString:

{\"phonetype\":\"N95\",\"cat\":\"WP\"}

Now I want to convert it into JSON Object. I

19条回答
  •  后悔当初
    2020-11-22 01:26

    Using org.json

    If you have a String containing JSON format text, then you can get JSON Object by following steps:

    String jsonString = "{\"phonetype\":\"N95\",\"cat\":\"WP\"}";
    JSONObject jsonObj = null;
        try {
            jsonObj = new JSONObject(jsonString);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    

    Now to access the phonetype

    Sysout.out.println(jsonObject.getString("phonetype"));
    

提交回复
热议问题