繁体   English   中英

如何从hashmap json对象打印值

[英]How do I print value from hashmap json object

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

我想从正在创建的哈希图中打印术语和类型。 这是我的代码,这是我对URL的回复{“ neighborhoods”:{“ label”:“ abc”,“ value”:[]},“ communities”:{“ label”:“ xyz”,“ value”:{ “ 83”:{“ label”:“加利福尼亚州旧金山湾区94538”,“值”:83,“类型”:“社区”,“术语”:“ abc”},“ 94”:{“标签” :“加利福尼亚州旧金山湾区94538”,“值”:94,“类型”:“社区”,“期限”:“ 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);
      }

}

{“ neighborhoods”:{“ label”:“ Neighborhoods”,“ value”:[]},“ communities”:{“ label”:“ Communities”,“ value”:{“ 83”:{“ label”:“ Mission Peaks-加利福尼亚州旧金山湾区94538”,“值”:83,“ type”:“ community”,“ term”:“ Mission Peaks”},“ 94”:{“ label”:“ Mission Peaks II-加利福尼亚州,旧金山湾区94538”,“值”:84,“类型”:“社区”,“期限”:“ Mission Peaks II”}}}}

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

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

暂无
暂无

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

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