簡體   English   中英

轉換 JSONObject 中的緩沖閱讀器 (json-simple)

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

我在 GET 中調用 Rest API,我需要解析響應並獲取鍵的值。 我在用着:

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

這是我的代碼:

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

但是在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

非常感謝。

您應該使用JSONParser從 String 獲取數據:

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

JSONObject用於通過toJSONString()方法將其數據序列化為 JSON 字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM