首页 > 解决方案 > 如何从 hashmap json 对象打印值

问题描述

我正在学习java中的jsonobject。我想从 json 对象打印值。我正在读取 URL 并将其存储到 hashmap 中。但现在我确实打印了诸如术语和类型之类的颗粒值。

我想从我正在创建的哈希图中打印术语和类型。这是我的代码这是我从 URL { "neighborhoods": { "label": "abc", "value": [] }, "communities": { "label": "xyz", "value": { “83”:{“标签”:“旧金山湾区,加利福尼亚州 94538”,“值”:83,“类型”:“社区”,“术语”:“abc”},“94”:{“标签” :“旧金山湾区,加利福尼亚州 94538”,“值”:94,“类型”:“社区”,“术语”:

  public static void main(String[] args) {
    try {
  String url = "Removing URL and added json response which getting above the code";
      URL obj = new URL(url);
      HttpURLConnection con = (HttpURLConnection) obj.openConnection();
      int responseCode = con.getResponseCode();
      System.out.println("\nSending 'GET' request to URL : " + url);
      System.out.println("Response Code : " + responseCode);
      BufferedReader in =new BufferedReader(
      new InputStreamReader(con.getInputStream()));
      String inputLine;
      StringBuffer response = new StringBuffer();
       while ((inputLine = in.readL***strong text***ine()) != null) {
         response.append(inputLine);
       } in .close();
       //print in String
        System.out.println(response.toString());
       JSONObject myresponse = new JSONObject(response.toString());
       System.out.println(myresponse);

       JSONObject neighborhoods_object = new JSONObject(myresponse.getJSONObject("neighborhoods").toString());
       System.out.println("\n\nNeighborhoods Objects -" + neighborhoods_object);

       JSONObject communities_object = new JSONObject(myresponse.getJSONObject("communities").toString());
       System.out.println("\nCommunities Objects -" + communities_object);
       System.out.println("Text from Label " + communities_object.getString("label"));

       JSONObject value_object1 = new JSONObject(communities_object.getJSONObject("value").toString());
       System.out.println("\nCommunities Objects and within that Value Object-" + value_object1);

       Map<String, Object> result = new HashMap<String, Object>();
       System.out.println("Length " + value_object1.length());

       Iterator<String> keysItr = value_object1.keys();
       while(keysItr.hasNext()) {
            String key = keysItr.next();
            Object value = value_object1.get(key);
            result.put(key, value);
       }

       for(Map.Entry<String, Object> entry : result.entrySet())
       {
           System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
    //   JSONObject neighborhoods_object2 = new JSONObject(value_object1.getJSONObject("83").toString());

    //   System.out.println("\n\nNeighborhoods Objects 2-" + neighborhoods_object2);
    //
    //   JSONObject neighborhoods_object3 = new JSONObject(value_object1.getJSONObject("83").toString());
    //   System.out.println("Value of Label-" + neighborhoods_object3.getString("label"));
    //   System.out.println("Value of -" + neighborhoods_object3.getInt("value"));
    //   System.out.println("Type -" + neighborhoods_object3.getString("type"));
    //   System.out.println("Short Term-" + neighborhoods_object3.getString("term"));
       }


      } catch(Exception e) {
        System.out.println(e);
      }

}

{“社区”:{“标签”:“社区”,“价值”:[]},“社区”:{“标签”:“社区”,“价值”:{“83”:{“标签”:“ Mission Peaks - San Francisco Bay Area, California 94538", "value": 83, "type": "community", "term": "Mission Peaks" }, "94": { "label": "Mission Peaks II - San Francisco Bay Area, California 94538", "value": 84, "type": "community", "term": "Mission Peaks II"} } } }

标签: javajsonhashmap

解决方案


这足以让您从 json 对象中获取详细信息

urlConnection.connect();
is = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
      sb.append(line + "\n");
}
is.close();
json = sb.toString();
jObj = new JSONObject(json);

之后只需使用

jObj.getString("key");

或您想要的任何类型的变量


推荐阅读