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

StackOverflow https://stackoverflow.com/questions/8253017

Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top