toJSONString() is undefined for the type JSONObject

前端 未结 4 1530
情歌与酒
情歌与酒 2021-01-02 15:21

My code to create a new JSONObject and write to a file:

JSONObject obj = new JSONObject();
obj.put(\"name\", \"abcd\");
obj.put(\"age\", new Integer(100));
J         


        
4条回答
  •  走了就别回头了
    2021-01-02 15:58

    The method toJSONString() is from json-simple but I guess you are using org.json.

    org.json have an alternative to toJSONString(). You can simply use: obj.toString(1).

    The difference to the toString() method is, that if you pass "1" as parameter org.json automatically will format and beautify your JSON. So you don't have only one single compressed line of JSON in your file.

提交回复
热议问题