第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

無(wú)法在 json 數(shù)組中打印 json 對(duì)象

無(wú)法在 json 數(shù)組中打印 json 對(duì)象

尚方寶劍之說(shuō) 2021-11-11 18:23:21
這是我正在使用的 json。我需要打印數(shù)組description內(nèi)的對(duì)象weather。JSONArray[2] not found編譯時(shí)出現(xiàn)異常。我正在使用 java-json。{  "coord": {    "lon": 72.85,    "lat": 19.01  },  "weather": [    {      "id": 721,      "main": "Haze",      "description": "haze",      "icon": "50n"    }  ],  "base": "stations",  "main": {    "temp": 303.15,    "pressure": 1009,    "humidity": 74,    "temp_min": 303.15,    "temp_max": 303.15  },  "visibility": 3000,  "wind": {    "speed": 2.1,    "deg": 360  },  "clouds": {    "all": 20  },  "dt": 1539273600,  "sys": {    "type": 1,    "id": 7761,    "message": 0.0642,    "country": "IN",    "sunrise": 1539219701,    "sunset": 1539262109  },  "id": 1275339,  "name": "Mumbai",  "cod": 200}這是代碼——import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import org.json.JSONObject;import org.json.JSONArray;class Send_HTTP_Request2 {    public static void main(String[] args) {     try {         Send_HTTP_Request2.call_me();        } catch (Exception e) {         e.printStackTrace();       }     } static void call_me() throws Exception {     String url = "http://api.openweathermap.org/data/2.5/weather?id=1275339&APPID=77056fb4e0ba03b117487193c37c90d2";     URL obj = new URL(url);     HttpURLConnection con = (HttpURLConnection) obj.openConnection();     int responseCode = con.getResponseCode();     System.out.println("\nSending 'GET' request to URL : " + url);     BufferedReader in = new BufferedReader(             new InputStreamReader(con.getInputStream()));     String inputLine;     StringBuffer response = new StringBuffer();     while ((inputLine = in.readLine()) != null) {        response.append(inputLine);     }     in.close();     JSONObject myResponse = new JSONObject(response.toString());         JSONArray jrr= myResponse.getJSONArray("weather");          System.out.println("CITY-"+myResponse.getString("name"));         JSONObject desc = jrr.getJSONObject(2);         System.out.println(desc);}}
查看完整描述

1 回答

?
吃雞游戲

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

對(duì)于getJSONObject(int index)(此處鏈接到 Javadoc)的 JSONArray 方法


獲取數(shù)組內(nèi)的 JSONObject 是正確的,但是獲取了錯(cuò)誤的索引,在本例中為 0,因?yàn)樵?Java 中,索引 0 是數(shù)組的第一項(xiàng)。(更多關(guān)于數(shù)組和索引在這里)


然后您只需調(diào)用desc.getString("description")并將其分配給一個(gè)字符串,因?yàn)槊枋鲦I是一個(gè)字符串類(lèi)型。


所以更具體地說(shuō),你會(huì)做一些鏈接這個(gè)(假設(shè)我們沒(méi)有檢查空值或使用 for 循環(huán)或任何東西遍歷數(shù)組):


JSONObject myResponse = new JSONObject(response.toString());

JSONArray jrr= myResponse.getJSONArray("weather");

System.out.println("CITY-"+myResponse.getString("name"));

JSONObject weatherObj = jrr.getJSONObject(0);

String desc = weatherObj.getString("description");

System.out.println(desc);

希望這可以幫助!


查看完整回答
反對(duì) 回復(fù) 2021-11-11
  • 1 回答
  • 0 關(guān)注
  • 253 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)