24

我正在使用 json-rpc-1.0.jar。下面是我的代码。我需要将 InputStream 对象转换为 JSON,因为响应是 JSON。

我确实验证了从 Zappos API 获得的 json 响应。这是有效的。

PrintWriter out = resp.getWriter();
String jsonString = null;
URL url = new URL("http://api.zappos.com/Search?term=boots&key=my_key");
InputStream inputStream = url.openConnection().getInputStream();
resp.setContentType("application/json");

JSONSerializer jsonSerializer = new JSONSerializer();
try {
   jsonString = jsonSerializer.toJSON(inputStream);
} catch (MarshallException e) {
 e.printStackTrace();
    }
out.print(jsonString);

我得到下面提到的异常:

com.metaparadigm.jsonrpc.MarshallException: can't marshall sun.net.www.protocol.http.HttpURLConnection$HttpInputStream
    at com.metaparadigm.jsonrpc.JSONSerializer.marshall(JSONSerializer.java:251)
    at com.metaparadigm.jsonrpc.JSONSerializer.toJSON(JSONSerializer.java:259)
    at Communicator.doGet(Communicator.java:33)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
    at filters.ExampleFilter.doFilter(ExampleFilter.java:149)
4

3 回答 3

72

使用 Jackson JSON 解析器。

参考 -杰克逊之家

你唯一需要做的就是——

ObjectMapper mapper = new ObjectMapper();
Map<String, Object> jsonMap = mapper.readValue(inputStream, Map.class);

现在jsonMap将包含 JSON。

于 2013-09-13T20:49:27.510 回答
4

ObjectMapper.readTree(InputStream) 很容易让你用 JsonNodes 获得嵌套的 JSON。

public void testMakeCall() throws IOException {
    URL url = new URL("https://api.coindesk.com/v1/bpi/historical/close.json?start=2010-07-17&end=2018-07-03");
    HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
    httpcon.addRequestProperty("User-Agent", "Mozilla/4.0");
    InputStream is = httpcon.getInputStream();

    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonMap = mapper.readTree(is);
        JsonNode bpi = jsonMap.get("bpi");
        JsonNode day1 = bpi.get("2010-07-18");

        System.out.println(bpi.toString());
        System.out.println(day1.toString());
    } finally {
        is.close();
    }
}

结果:

{"2010-07-18":0.0858,"2010-07-19":0.0808,...}

0.0858

于 2018-07-04T03:49:41.420 回答
0

通过将输出设置为更好地节省内存Stream<JsonNode>


    private fun InputStream.toJsonNodeStream(): Stream<JsonNode> {
        return StreamSupport.stream(
                Spliterators.spliteratorUnknownSize(this.toJsonNodeIterator(), Spliterator.ORDERED),
                false
        )
    }

    private fun InputStream.toJsonNodeIterator(): Iterator<JsonNode> {
        val jsonParser = objectMapper.factory.createParser(this)

        return object: Iterator<JsonNode> {

            override fun hasNext(): Boolean {
                var token = jsonParser.nextToken()
                while (token != null) {
                    if (token == JsonToken.START_OBJECT) {
                        return true
                    }
                    token = jsonParser.nextToken()
                }
                return false
            }

            override fun next(): JsonNode {
                return jsonParser.readValueAsTree()
            }
        }
    }

于 2019-11-24T00:19:58.017 回答