无法将具有换行符的字符串转换为 JSONArray

Can't convert string have new line characters to JSONArray

我在从具有换行符的字符串创建 JSONArray 时遇到问题示例:

 String text = "[" +
            "'test line 1 \n" +
            "test line 2 \n" +
            "line 3', " +
            "'255.255.255.240' ," +
                "'<Not Set>'" +
            "] ";
 JSONArray tmpArray = new JSONArray(text); 

会有例外:

Exception in thread "main" org.json.JSONException: Unterminated string at character 15 of ['test line 1 

有没有办法解决这个问题?

谢谢,

您应该像在 \n 中那样转义 \,然后它将起作用。另请注意,您还需要转义 '

 String text = "[" +
        "'test line 1 \n" +
        "test line 2 \n" +
        "line 3', " +
        "'255.255.255.240' ," +
            "'<Not Set>'" +
        "] ";