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

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

請(qǐng)問(wèn)使用Java訪問(wèn)JSONArray中的項(xiàng)成員

請(qǐng)問(wèn)使用Java訪問(wèn)JSONArray中的項(xiàng)成員

慕村225694 2019-09-05 10:05:49
使用Java訪問(wèn)JSONArray中的項(xiàng)成員我剛剛開(kāi)始在java中使用json。我不確定如何在JSONArray中訪問(wèn)字符串值。例如,我的json看起來(lái)像這樣:{   "locations": {     "record": [       {         "id": 8817,         "loc": "NEW YORK CITY"       },       {         "id": 2873,         "loc": "UNITED STATES"       },       {         "id": 1501         "loc": "NEW YORK STATE"       }     ]   }}我的代碼:JSONObject req = new JSONObject(join(loadStrings(data.json),""));JSONObject locs = req.getJSONObject("locations");JSONArray recs = locs.getJSONArray("record");此時(shí)我可以訪問(wèn)“記錄”JSONArray,但我不確定如何在for循環(huán)中獲取“id”和“l(fā)oc”值。對(duì)不起,如果這個(gè)描述不太清楚,我對(duì)編程有點(diǎn)新意。
查看完整描述

3 回答

?
月關(guān)寶盒

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

您是否嘗試過(guò)使用JSONArray.getJSONObject(int)和JSONArray.length()來(lái)創(chuàng)建for循環(huán):

for (int i = 0; i < recs.length(); ++i) {
    JSONObject rec = recs.getJSONObject(i);
    int id = rec.getInt("id");
    String loc = rec.getString("loc");
    // ...}



查看完整回答
反對(duì) 回復(fù) 2019-09-09
?
陪伴而非守候

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

一個(gè)org.json.JSONArray不迭代。
以下是我在net.sf.json.JSONArray中處理元素的方法:

    JSONArray lineItems = jsonObject.getJSONArray("lineItems");
    for (Object o : lineItems) {
        JSONObject jsonLineItem = (JSONObject) o;
        String key = jsonLineItem.getString("key");
        String value = jsonLineItem.getString("value");
        ...
    }

效果很好...... :)



查看完整回答
反對(duì) 回復(fù) 2019-09-09
?
藍(lán)山帝景

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

Java 8在近二十年后進(jìn)入市場(chǎng),以下是org.json.JSONArray使用java8 Stream API 進(jìn)行迭代的方法。

import org.json.JSONArray;import org.json.JSONObject;@Testpublic void access_org_JsonArray() {
    //Given: array
    JSONArray jsonArray = new JSONArray(Arrays.asList(new JSONObject(
                    new HashMap() {{
                        put("a", 100);
                        put("b", 200);
                    }}
            ),
            new JSONObject(
                    new HashMap() {{
                        put("a", 300);
                        put("b", 400);
                    }}
            )));
    //Then: convert to List<JSONObject>
    List<JSONObject> jsonItems = IntStream.range(0, jsonArray.length())
            .mapToObj(index -> (JSONObject) jsonArray.get(index))
            .collect(Collectors.toList());
    // you can access the array elements now
    jsonItems.forEach(arrayElement -> System.out.println(arrayElement.get("a")));
    // prints 100, 300}

如果迭代只有一次,(不需要.collect

    IntStream.range(0, jsonArray.length())
            .mapToObj(index -> (JSONObject) jsonArray.get(index))
            .forEach(item -> {
               System.out.println(item);
            });



查看完整回答
反對(duì) 回復(fù) 2019-09-09
  • 3 回答
  • 0 關(guān)注
  • 359 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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