为什么我的json文件中的换行符显示为字符“n”(why are the newline characters in my json file showing up as the character

编程入门 行业动态 更新时间:2024-10-24 16:33:31
为什么我的json文件中的换行符显示为字符“n”(why are the newline characters in my json file showing up as the character “n”)

我有一个Android应用程序,它应该从Web获取一个json文件并解析它。 我将json文件作为直接文件上传到我的网站,扩展名为.json

我的应用程序获取此文件,但当我打印出其内容时,应该有一个换行符的任何地方,而是有一个“n”,当然json解析器不喜欢这样,我得到一个例外。

更新:在查看解析输入流的代码后,我发现了问题:

BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "n"); }

出于某种原因,在这个代码中,我的其他应用程序完美运行,我将“n”添加到“\ n”上! 我想知道我是否意外删除了反斜杠。

I have a android app thats supposed to fetch a json file from the web and pars it. I uploaded the json file onto my website as direct file with the extension .json

my app fetches this file, but when I print out its contents, anywhere where there should be a newline character, there is a "n" instead, and naturally the json parser doesnt like this and I get an exception.

UPDATE: after looking at my code that parses the input stream I found the problem:

BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "n"); }

for some reason, in this code, which was working flawlessly for my other app, I am appending "n" instead on "\n"!!! I wonder if I deleted the backslash by accident.

最满意答案

你必须通过使用来逃避斜线字符

\\n

在json里面。

稍后您可以使用此代码从json解析字符串,

public String escapeJavaString(String st) { StringBuilder sb = new StringBuilder(st.length()); for (int i = 0; i < st.length(); i++) { char ch = st.charAt(i); if (ch == '\\') { char nextChar = (i == st.length() - 1) ? '\\' : st .charAt(i + 1); // Octal escape? if (nextChar >= '0' && nextChar <= '7') { String code = "" + nextChar; i++; if ((i < st.length() - 1) && st.charAt(i + 1) >= '0' && st.charAt(i + 1) <= '7') { code += st.charAt(i + 1); i++; if ((i < st.length() - 1) && st.charAt(i + 1) >= '0' && st.charAt(i + 1) <= '7') { code += st.charAt(i + 1); i++; } } sb.append((char) Integer.parseInt(code, 8)); continue; } switch (nextChar) { case '\\': ch = '\\'; break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case '\"': ch = '\"'; break; case '\'': ch = '\''; break; // Hex Unicode: u???? case 'u': if (i >= st.length() - 5) { ch = 'u'; break; } int code = Integer.parseInt( "" + st.charAt(i + 2) + st.charAt(i + 3) + st.charAt(i + 4) + st.charAt(i + 5), 16); sb.append(Character.toChars(code)); i += 5; continue; } i++; } sb.append(ch); } return sb.toString(); }

看看这里获得更多帮助。

You would have to escape the slash character by using

\\n

inside the json.

Later on you can use this code for parsing the string from json,

public String escapeJavaString(String st) { StringBuilder sb = new StringBuilder(st.length()); for (int i = 0; i < st.length(); i++) { char ch = st.charAt(i); if (ch == '\\') { char nextChar = (i == st.length() - 1) ? '\\' : st .charAt(i + 1); // Octal escape? if (nextChar >= '0' && nextChar <= '7') { String code = "" + nextChar; i++; if ((i < st.length() - 1) && st.charAt(i + 1) >= '0' && st.charAt(i + 1) <= '7') { code += st.charAt(i + 1); i++; if ((i < st.length() - 1) && st.charAt(i + 1) >= '0' && st.charAt(i + 1) <= '7') { code += st.charAt(i + 1); i++; } } sb.append((char) Integer.parseInt(code, 8)); continue; } switch (nextChar) { case '\\': ch = '\\'; break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case '\"': ch = '\"'; break; case '\'': ch = '\''; break; // Hex Unicode: u???? case 'u': if (i >= st.length() - 5) { ch = 'u'; break; } int code = Integer.parseInt( "" + st.charAt(i + 2) + st.charAt(i + 3) + st.charAt(i + 4) + st.charAt(i + 5), 16); sb.append(Character.toChars(code)); i += 5; continue; } i++; } sb.append(ch); } return sb.toString(); }

Have a look here for more help.

更多推荐

本文发布于:2023-07-25 05:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1256733.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   文件   换行符   json   newline

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!