Я изучаю jsonobject в Java.Я хочу напечатать значение из объекта JSON.Я читаю из URL и сохраняю в hashmap.но теперь я печатаю значение частиц, такое как термин и тип.
Я ХОЧУ ПЕЧАТЬ СРОК И ТИП ИЗ МОДУЛЯ, КОТОРЫЙ Я СОЗДАЮ.Вот мой код Вот мой ответ от URL {"окрестности": {"label": "abc", "value": []}, "community": {"label": "xyz", "value": {"83": {"label": "Область залива Сан-Франциско, Калифорния 94538", "value": 83, "type": "community", "term": "abc"}, "94": {"label": "Область залива Сан-Франциско, Калифорния 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);
}
}
{"окрестности": {"метка": "Окрестности", "значение": []}, "сообщества": {"метка": "сообщества", "значение": {"83": {"метка": "Mission Peaks - San Francisco Bay Area, California 94538", "value": 83, "type": "community", "term": "Mission Peaks"}, "94": {"label": "Mission PeaksII - Залив Сан-Франциско, Калифорния, 94538 "," value ": 84," type ":" community "," term ":" Mission Peaks II "}}}}