将BufferedReader转换为JSONObject或Map

时间:2014-10-14 10:36:29

标签: java json map httprequest

我的代码向服务器发送GET请求,

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("User-Agent", USER_AGENT); 
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

我得到一个打印的BufferedReader对象,

{  
   "status": "ERROR",    
   "errorCode": "MISSING_PARAMS",    
   "errorMessage": null,    
   "requestId": "20141014181739_11625805172",    
   "downstreamModuleErrorCode": null,    
   "object": [        
     "activity_code",        
     "activity_name",
     "points",
     "frequency",
     "strategy",
     "vsa_app_access_token"    
   ]
}

我想从中获取JSONOBject或Map。我尝试将其转换为String并对其进行操作。但这并不容易。请帮忙。

5 个答案:

答案 0 :(得分:22)

首先将其作为字符串。您可以使用自定义库来

 String message = org.apache.commons.io.IOUtils.toString(rd);

或StringBuilder

    StringBuilder sb = new StringBuilder();

    String line;
    br = new BufferedReader(new InputStreamReader(is));
    while ((line = br.readLine()) != null) {
        sb.append(line);
    }

然后你可以解析它。因为它是" {" (数组以[]开头和结尾)你需要创建一个JSONObject。

JSONObject json = new JSONObject(sb.toString());

然后您可以使用

访问您的元素
//{ "status": "ERROR", "errorCode": "MISSING_PARAMS", "errorMessage": null, "requestId": "20141014181739_11625805172", "downstreamModuleErrorCode": null, "object": [ "activity_code", "activity_name", "points", "frequency", "strategy", "vsa_app_access_token" ]}

json.getString("status");

或带

的数组
JSONArray jsonArray = new JSONArray(json.getString("object"));

或者您使用方法getJSONArray()

JSONArray jsonArray = json.getJSONArray("object");

答案 1 :(得分:4)

InputStream inputStream = connection.getInputStream();

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder builder = new StringBuilder();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        builder.append(line + "\n");
        }

JSONArray jsonArray = new JSONArray(builder.toString());
for (int i = 0; i < jsonArray.length(); i++) {
     JSONObject json = jsonArray.getJSONObject(i);

    if (!json.get("object").equals(null)) {
        JSONArray objectJsonArray = json.getJSONArray("object");
            for (int i = 0; i < objectJsonArray.length(); i++) {
                JSONObject json = objectJsonArray.getJSONObject(i);     
            }       
    }
}

希望它有所帮助。

答案 2 :(得分:1)

那呢:

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
JsonReader reader = Json.createReader(in);
JsonObject object = reader.readObject();
String s = object.toString();

答案 3 :(得分:0)

使用此lib(org.json),您可以将字符串转换为json对象:http://www.json.org/java/

JSONObject jsonObj = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");

答案 4 :(得分:0)

org.json的最新版本中,您可以执行以下操作:

JSONTokener tokener = new JSONTokener(bufferedReader);
JSONObject json = new JSONObject(tokener);

作为快速证明,它可以与org.json:json:20190722一起使用:

byte[] b = "{ \"active\" : true }".getBytes(StandardCharsets.UTF_8);
InputStream is = new ByteArrayInputStream(b);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
JSONTokener tokener = new JSONTokener(bufferedReader);
JSONObject json = new JSONObject(tokener);