How to convert Java String to JSON Object

后端 未结 5 1285
暖寄归人
暖寄归人 2020-12-29 12:07

This question has been asked earlier, but I am unable to figure out the error in my code from the responses to those questions.


I am trying to convert a java s

5条回答
  •  春和景丽
    2020-12-29 12:42

    The string that you pass to the constructor JSONObject has to be escaped with quote():

    public static java.lang.String quote(java.lang.String string)
    

    Your code would now be:

    JSONObject jsonObj = new JSONObject.quote(jsonString.toString());
    System.out.println(jsonString);
    System.out.println("---------------------------");
    System.out.println(jsonObj);
    

提交回复
热议问题