Skip to content Skip to sidebar Skip to footer

Why Are The Newline Characters In My Json File Showing Up As The Character "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 t

Solution 1:

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) {

    StringBuildersb=newStringBuilder(st.length());

    for (inti=0; i < st.length(); i++) {
        charch= st.charAt(i);
        if (ch == '\\') {
            charnextChar= (i == st.length() - 1) ? '\\' : st
                    .charAt(i + 1);
            // Octal escape?if (nextChar >= '0' && nextChar <= '7') {
                Stringcode="" + 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;
                }
                intcode= 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.

Solution 2:

My guess would be that you have to escape the slash character. That is in your website you should put "\\n" inside the json object for each new line.

Post a Comment for "Why Are The Newline Characters In My Json File Showing Up As The Character "n""