【问题标题】:How do I print value from hashmap json object如何从 hashmap json 对象打印值
【发布时间】:2019-01-31 20:53:19
【问题描述】:

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

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

}

{ “社区”:{ "label": "社区", “价值”: [] }, “社区”:{ “标签”:“社区”, “价值”: { “83”:{ "label": "Mission Peaks - San Francisco Bay Area, California 94538", “价值”:83, “类型”:“社区”, “术语”:“任务峰” }, “94”:{ "label": "Mission Peaks II - San Francisco Bay Area, California 94538", “价值”:84, “类型”:“社区”, “术语”:“任务峰 II” } } } }

【问题讨论】:

  • 什么是术语和类型?我在您的代码中没有看到该名称的任何内容。
  • 即在value_object1 JSONObject中。所以我正在创建带有键和对象的哈希图,并从中打印出来。
  • 只要您的地图是&lt;String, Object&gt; 类型,这是不可能的。如果您希望能够访问数据,则需要让编译器知道数据的样子。

标签: java json hashmap


【解决方案1】:

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

或者你想要的任何类型的变量

【讨论】:

  • 我可以打印,但是有嵌套的 json 对象。这是回应。我删除了网址
  • 我相信是jsonArray。尝试将每个键作为 'jsonObject.getJSONArray("key")' 获取,它会帮助你得到它
  • 但无论如何,如果它是一个嵌套的 json 对象,你可以只做 'jsonObject.getJSONObject("key") '
猜你喜欢
  • 1970-01-01
  • 2020-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-20
  • 2017-12-14
  • 1970-01-01
相关资源
最近更新 更多