I have a JSON Service that returns results with "\n" when InputStreamReader reads the result "\" is gone

168 views Asked by At

I have a JSON Service that returns results with "\n" when InputStreamReader reads the result the "\" is gone and leaving the "n" in the String.

I'm having trouble converting the "\n" to next line to be displayed in a TextArea.

For example:

Service Result: Hello\nWorld
PassThrough InputStreamReader: HellonWorld
TextArea Output: HellonWorld

I need it to be:

Service Result: Hello\nWorld
PassThrough InputStreamReader: Hello
World
TextArea Output: Hello
World

Please help me on how to result this problem.

I'm using LWUIT and is developing for a J2ME device.

2

There are 2 answers

0
bernardnapoleon On BEST ANSWER

This is not the best way to do this but here it is:

Add the following lines to the JSONParser.java

else if (c == 'n') {
    c = '\n';
}

below

c = (char) i.read();
if (c == 'u') {
    String unicode = "" + ((char) i.read()) + ((char) i.read()) + ((char) i.read()) + ((char) i.read());
    try {
        c = (char) Integer.parseInt(unicode, 16);
    } catch (NumberFormatException err) {
        // problem in parsing the u notation!
        err.printStackTrace();
        System.out.println("Error in parsing \\u" + unicode);
    }
}
1
Shai Almog On

Can you paste the exact JSON string returned. If the data is within double quotes " then it shouldn't be modified at all with the exception of decoding the \u notations.