无法将字符串转换为新行字符到JSONArray

时间:2015-01-30 05:24:42

标签: java json string exception converter

我有从字符串创建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 

是否有办法解决问题?

谢谢,

1 个答案:

答案 0 :(得分:0)

你应该像\一样逃避\\n,然后就可以了。另请注意,您还需要转义'

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