如何从哈希图json对象中打印值

时间:2019-01-31 20:53:19

标签: java json hashmap

我正在学习Java中的jsonobject。我想从json对象打印值。我正在从URL阅读并存储到hashmap中。但是现在我要打印术语和类型之类的颗粒值。

我想从正在创建的HASHMAP中打印术语和类型。 这是我的代码 这是我对URL的回复 {     “邻居”:{         “ label”:“ abc”,         “值”:[]     },     “社区”:{         “ label”:“ xyz”,         “值”:{             “ 83”:{                 “标签”:“加利福尼亚州旧金山湾区94538”,                 “值”:83,                 “ type”:“社区”,                 “ term”:“ abc”             },             “ 94”:{                 “标签”:“加利福尼亚州旧金山湾区94538”,                 “值”:94,                 “ type”:“社区”,                 “ term”:“ II”             }         }     } }

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

}

{     “邻居”:{         “ label”:“邻居”,         “值”:[]     },     “社区”:{         “ label”:“社区”,         “值”:{             “ 83”:{                 “标签”:“ Mission Peaks-加利福尼亚州旧金山湾区94538”,                 “值”:83,                 “ type”:“社区”,                 “ term”:“ Mission Peaks”             },             “ 94”:{                 “标签”:“ Mission Peaks II-加利福尼亚州旧金山湾区94538”,                 “值”:84,                 “ type”:“社区”,                 “ term”:“ Mission Peaks II”             }         }     } }

1 个答案:

答案 0 :(得分:0)

这足以让您从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");

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

相关问题