简体   繁体   English

转换 JSONObject 中的缓冲阅读器 (json-simple)

[英]Convert Buffered Reader in JSONObject (json-simple)

i'm calling a Rest API in GET, and i need to parse the response and take the value of a key.我在 GET 中调用 Rest API,我需要解析响应并获取键的值。 I'm using:我在用着:

<dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>

This is my code:这是我的代码:

if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder sb = new StringBuilder();
            while ((readLine = in.readLine()) != null) {
                sb.append(readLine);
            }
            JSONObject jsonObject = new JSONObject(sb.toString());
            jsonObject.get();
            in.close();

But in JSONObject jsonObject = new JSONObject(sb.toString());但是在JSONObject jsonObject = new JSONObject(sb.toString()); generate a error Error:(32, 63) java: incompatible types: java.lang.String cannot be converted to java.util.Map generate a error Error:(32, 63) java: incompatible types: java.lang.String cannot be converted to java.util.Map

Thanks so much.非常感谢。

You should be using JSONParser to get the data from String:您应该使用JSONParser从 String 获取数据:

JSONParser parser = new JSONParser(); 
JSONObject json = (JSONObject) parser.parse(sb.toString());

JSONObject is used to serialize its data into JSON string via toJSONString() method. JSONObject用于通过toJSONString()方法将其数据序列化为 JSON 字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM