为什么在我的JSON文件中的换行符显示为字符" N" [英] why are the newline characters in my json file showing up as the character "n"

查看:241
本文介绍了为什么在我的JSON文件中的换行符显示为字符" N"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该抓取从网上一个JSON文件和收杆成为一个Android应用程序多数民众赞成。
我上传的JSON文件到我的网站为扩展以.json

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

我的应用程序读取这个文件,但是当我打印出来的内容,任何地方应该有一个换行符,有一个N,而是和自然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.

更新:
看着我的code,它解析输入流后,我发现这个问题:

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");
        }

由于某种原因,在此code,这是完美的工作为我的应用程序等,我追加N而不是在\\ 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.

推荐答案

您将不得不使用逃脱斜线字符

You would have to escape the slash character by using

\\\\ñ

在里面JSON。结果

随后,您可以使用此code从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();
}

看一看这里获得更多帮助。

这篇关于为什么在我的JSON文件中的换行符显示为字符&QUOT; N&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆