本文介绍了如何将application / json对象解析为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以编程方式导航到返回application / json格式的站点。我似乎无法读取HttpURLConnection中返回的json。我正在使用Jackson将JSON解组为java对象。代码是:

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

当我打印sb.toString()时,我会看到有趣的字符和unicodes。我应该得到一个结构良好的字符串。产生的异常是:

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>

但是当我这么做时

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

我也尝试将输入流直接传递给jackson,但失败的结果相同。

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

我知道如何以可查看的字符串格式从URLConn中检索JSON,以便我可以传递它到Jacson?

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

谢谢

推荐答案

默认情况下, HttpURLConnection 将编码设置为 gzip 。一旦我禁用它,我就能从流中读取字符串:

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

这篇关于如何将application / json对象解析为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 05:38