How to convert jsonString to JSONObject in Java

前端 未结 19 3335
一生所求
一生所求 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:28

    Better Go with more simpler way by using org.json lib. Just do a very simple approach as below:

    JSONObject obj = new JSONObject();
    obj.put("phonetype", "N95");
    obj.put("cat", "WP");
    

    Now obj is your converted JSONObject form of your respective String. This is in case if you have name-value pairs.

    For a string you can directly pass to the constructor of JSONObject. If it'll be a valid json String, then okay otherwise it'll throw an exception.

提交回复
热议问题