How to parse an application/json object into a String

jabawaba picture jabawaba · Nov 11, 2011 · Viewed 11k times · Source

I am programmatically navigating to a site that returns application/json format. I can't seem to read the json returned in the HttpURLConnection. I am using Jackson to demarshall the JSON into the java object. The code is:

InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
    sb.append(line);
    line = br.readLine();
}
geoLocation = (new ObjectMapper()).readValue(sb.toString(), GeoLocation.class);

When I print sb.toString(), I get funny looking characters and unicodes. I should get a well formed String. The resulting exception is:

org.codehaus.jackson.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@3f6dadf9; line: 1, column: 2]
    at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1291)
    at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:385)

For example, if my url is:

http://api.ipinfodb.com/v3/ip-city/?key=<mykeyhere>&ip=38.111.145.101&format=xml

I get the following inside the Browser window:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <statusCode>OK</statusCode>
    <statusMessage></statusMessage>
    <ipAddress>38.111.145.101</ipAddress>
    <countryCode>US</countryCode>
    <countryName>UNITED STATES</countryName>
    <regionName>CALIFORNIA</regionName>
    <cityName>OAKLAND</cityName>
    <zipCode>94601</zipCode>
    <latitude>37.7993</latitude>
    <longitude>-122.24</longitude>
    <timeZone>-08:00</timeZone>
</Response>

But when I do

http://api.ipinfodb.com/v3/ip-city/?key=<mykeyhere>&ip=38.111.145.101&format=json

It prompts me to download a file. Once you open the file after it has been downloaded, it contains:

{
    "statusCode" : "OK",
    "statusMessage" : "",
    "ipAddress" : "38.111.145.101",
    "countryCode" : "US",
    "countryName" : "UNITED STATES",
    "regionName" : "CALIFORNIA",
    "cityName" : "OAKLAND",
    "zipCode" : "94601",
    "latitude" : "37.7993",
    "longitude" : "-122.24",
    "timeZone" : "-08:00"
}

I also tried passing the input stream directly to jackson but it failed with the same result.

geoLocation = (new ObjectMapper()).readValue(urlConn.getInputStream(), GeoLocation.class);

Any idea how I can retrieve the JSON from the URLConn in a viewable String format so I can pass it to Jacson?

Thank you

Answer

jabawaba picture jabawaba · Nov 11, 2011

By default, HttpURLConnection sets the encoding to gzip. Once I disabled it, I was able to read the String from the stream:

urlConn.setRequestProperty("Accept-Encoding", "identity");