gpt4 book ai didi

java - 如何从 hashmap json 对象打印值

转载 作者:太空宇宙 更新时间:2023-11-04 09:53:01 24 4
gpt4 key购买 nike

我正在学习java中的jsonobject。我想从 json 对象打印值。我正在从 URL 读取并将其存储到 hashmap 中。但现在我打印了具体的值,比如术语和类型。

我想从我正在创建的 HashMap 中打印术语和类型。这是我的代码这是我从 URL 得到的回复{ “社区”:{ “标签”:“abc”, “值(value)”: [] }, “社区”:{ “标签”:“xyz”, “值(value)”: { “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": "社区", “值(value)”: [] }, “社区”:{ "label": "社区", “值(value)”: { “83”:{ "label": "Mission Peaks - 旧金山湾区, 加利福尼亚州 94538", “值”:83, “类型”:“社区”, “术语”:“使命峰” }, “94”:{ "label": "Mission Peaks II - 旧金山湾区, 加利福尼亚州 94538", “值”:84, “类型”:“社区”, “术语”:“任务峰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");

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

关于java - 如何从 hashmap json 对象打印值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54469063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com