JSONObject和JSONArray的问题

时间:2013-10-17 01:38:54

标签: android

        JSONObject result = new JSONObject("{\"result\":{\"players\":[{\"account_id\":4294967295,\"player_slot\":0,\"hero_id\":66,\"item_0\":180,\"item_1\":92,\"item_2\":42,\"item_3\":0,\"item_4\":0,\"item_5\":46,\"kills\":0,\"deaths\":10,\"assists\":8,\"leaver_status\":0,\"gold\":311,\"last_hits\":28,\"denies\":0,\"gold_per_min\":200,\"xp_per_min\":316,\"gold_spent\":5743,\"hero_damage\":3441,\"tower_damage\":188,\"hero_healing\":3515,\"level\":15,\"ability_upgrades\":[{\"ability\":5330,\"time\":189,\"level\":1},{\"ability\":5328,\"time\":385,\"level\":2},{\"ability\":5330,\"time\":479,\"level\":3},{\"ability\":5328,\"time\":539,\"level\":4},{\"ability\":5330,\"time\":791,\"level\":5},{\"ability\":5331,\"time\":857,\"level\":6},{\"ability\":5330,\"time\":985,\"level\":7},{\"ability\":5329,\"time\":1068,\"level\":8},{\"ability\":5328,\"time\":1194,\"level\":9},{\"ability\":5328,\"time\":1251,\"level\":10},{\"ability\":5331,\"time\":1415,\"level\":11},{\"ability\":5329,\"time\":1933,\"level\":12},{\"ability\":5329,\"time\":1970,\"level\":13},{\"ability\":5329,\"time\":2109,\"level\":14},{\"ability\":5002,\"time\":2342,\"level\":15}]},{\"account_id\":4294967295,\"player_slot\":1,\"hero_id\":88,\"item_0\":202,\"item_1\":29,\"item_2\":44,\"item_3\":0,\"item_4\":0,\"item_5\":0,\"kills\":7,\"deaths\":8,\"assists\":7,\"leaver_status\":0,\"gold\":138,\"last_hits\":31,\"denies\":2,\"gold_per_min\":234,\"xp_per_min\":344,\"gold_spent\":6435,\"hero_damage\":9492,\"tower_damage\":74,\"hero_healing\":0,\"level\":15,\"ability_upgrades\":[{\"ability\":5462,\"time\":298,\"level\":1},{\"ability\":5464,\"time\":371,\"level\":2},{\"ability\":5462,\"time\":445,\"level\":3},{\"ability\":5463,\"time\":555,\"level\":4},{\"ability\":5462,\"time\":746,\"level\":5},{\"ability\":5465,\"time\":881,\"level\":6},{\"ability\":5462,\"time\":942,\"level\":7},{\"ability\":5463,\"time\":979,\"level\":8},{\"ability\":5464,\"time\":1225,\"level\":9},{\"ability\":5465,\"time\":1365,\"level\":10},{\"ability\":5464,\"time\":1367,\"level\":11},{\"ability\":5463,\"time\":1966,\"level\":12},{\"ability\":5464,\"time\":1967,\"level\":13},{\"ability\":5463,\"time\":2361,\"level\":14},{\"ability\":5002,\"time\":2482,\"level\":15}]},{\"account_id\":4294967295,\"player_slot\":2,\"hero_id\":13,\"item_0\":36,\"item_1\":41,\"item_2\":40,\"item_3\":104,\"item_4\":46,\"item_5\":63,\"kills\":3,\"deaths\":13,\"assists\":5,\"leaver_status\":0,\"gold\":1738,\"last_hits\":97,\"denies\":4,\"gold_per_min\":284,\"xp_per_min\":357,\"gold_spent\":6694,\"hero_damage\":6801,\"tower_damage\":26,\"hero_healing\":0,\"level\":16,\"ability_upgrades\":[{\"ability\":5069,\"time\":130,\"level\":1},{\"ability\":5071,\"time\":280,\"level\":2},{\"ability\":5072,\"time\":349,\"level\":3},{\"ability\":5069,\"time\":380,\"level\":4},{\"ability\":5071,\"time\":531,\"level\":5},{\"ability\":5073,\"time\":639,\"level\":6},{\"ability\":5069,\"time\":759,\"level\":7},{\"ability\":5069,\"time\":824,\"level\":8},{\"ability\":5071,\"time\":1119,\"level\":9},{\"ability\":5071,\"time\":1201,\"level\":10},{\"ability\":5073,\"time\":1319,\"level\":11},{\"ability\":5072,\"time\":1380,\"level\":12},{\"ability\":5072,\"time\":1429,\"level\":13},{\"ability\":5072,\"time\":1691,\"level\":14},{\"ability\":5002,\"time\":2168,\"level\":15},{\"ability\":5073,\"time\":2399,\"level\":16}]},{\"account_id\":4294967295,\"player_slot\":3,\"hero_id\":59,\"item_0\":79,\"item_1\":63,\"item_2\":43,\"item_3\":37,\"item_4\":32,\"item_5\":17,\"kills\":11,\"deaths\":9,\"assists\":4,\"leaver_status\":0,\"gold\":100,\"last_hits\":90,\"denies\":16,\"gold_per_min\":319,\"xp_per_min\":397,\"gold_spent\":11024,\"hero_damage\":18103,\"tower_damage\":913,\"hero_healing\":723,\"level\":17,\"ability_upgrades\":[{\"ability\":5272,\"time\":236,\"level\":1},{\"ability\":5273,\"time\":298,\"level\":2},{\"ability\":5272,\"time\":366,\"level\":3},{\"ability\":5273,\"time\":476,\"level\":4},{\"ability\":5272,\"time\":549,\"level\":5},{\"ability\":5274,\"time\":650,\"level\":6},{\"ability\":5272,\"time\":827,\"level\":7},{\"ability\":5273,\"time\":893,\"level\":8},{\"ability\":5273,\"time\":958,\"level\":9},{\"ability\":5271,\"time\":1058,\"level\":10},{\"ability\":5274,\"time\":1131,\"level\":11},{\"ability\":5271,\"time\":1262,\"level\":12},{\"ability\":5271,\"time\":1356,\"level\":13},{\"ability\":5271,\"time\":1574,\"level\":14},{\"ability\":5002,\"time\":1715,\"level\":15},{\"ability\":5274,\"time\":2099,\"level\":16},{\"ability\":5002,\"time\":2449,\"level\":17}]},{\"account_id\":4294967295,\"player_slot\":4,\"hero_id\":1,\"item_0\":11,\"item_1\":145,\"item_2\":63,\"item_3\":46,\"item_4\":147,\"item_5\":0,\"kills\":2,\"deaths\":6,\"assists\":3,\"leaver_status\":0,\"gold\":2658,\"last_hits\":258,\"denies\":15,\"gold_per_min\":442,\"xp_per_min\":535,\"gold_spent\":12454,\"hero_damage\":2703,\"tower_damage\":689,\"hero_healing\":0,\"level\":19,\"ability_upgrades\":[{\"ability\":5004,\"time\":141,\"level\":1},{\"ability\":5003,\"time\":258,\"level\":2},{\"ability\":5003,\"time\":345,\"level\":3},{\"ability\":5005,\"time\":444,\"level\":4},{\"ability\":5003,\"time\":551,\"level\":5},{\"ability\":5006,\"time\":693,\"level\":6},{\"ability\":5003,\"time\":785,\"level\":7},{\"ability\":5005,\"time\":935,\"level\":8},{\"ability\":5005,\"time\":1063,\"level\":9},{\"ability\":5005,\"time\":1070,\"level\":10},{\"ability\":5006,\"time\":1096,\"level\":11},{\"ability\":5004,\"time\":1436,\"level\":12},{\"ability\":5004,\"time\":1483,\"level\":13},{\"ability\":5004,\"time\":1565,\"level\":14},{\"ability\":5002,\"time\":1647,\"level\":15},{\"ability\":5006,\"time\":1728,\"level\":16},{\"ability\":5002,\"time\":1867,\"level\":17},{\"ability\":5002,\"time\":2023,\"level\":18},{\"ability\":5002,\"time\":2211,\"level\":19}]},{\"account_id\":4294967295,\"player_slot\":128,\"hero_id\":32,\"item_0\":208,\"item_1\":71,\"item_2\":170,\"item_3\":63,\"item_4\":22,\"item_5\":0,\"kills\":9,\"deaths\":4,\"assists\":10,\"leaver_status\":0,\"gold\":2738,\"last_hits\":93,\"denies\":3,\"gold_per_min\":399,\"xp_per_min\":441,\"gold_spent\":12586,\"hero_damage\":11004,\"tower_damage\":2995,\"hero_healing\":0,\"level\":17,\"ability_upgrades\":[{\"ability\":5143,\"time\":122,\"level\":1},{\"ability\":5144,\"time\":268,\"level\":2},{\"ability\":5144,\"time\":329,\"level\":3},{\"ability\":5143,\"time\":452,\"level\":4},{\"ability\":5142,\"time\":637,\"level\":5},{\"ability\":5145,\"time\":720,\"level\":6},{\"ability\":5143,\"time\":853,\"level\":7},{\"ability\":5144,\"time\":923,\"level\":8},{\"ability\":5144,\"time\":1108,\"level\":9},{\"ability\":5145,\"time\":1370,\"level\":10},{\"ability\":5142,\"time\":1371,\"level\":11},{\"ability\":5143,\"time\":1513,\"level\":12},{\"ability\":5142,\"time\":1632,\"level\":13},{\"ability\":5142,\"time\":1855,\"level\":14},{\"ability\":5002,\"time\":1928,\"level\":15},{\"ability\":5145,\"time\":2104,\"level\":16},{\"ability\":5002,\"time\":2139,\"level\":17}]},{\"account_id\":89395208,\"player_slot\":129,\"hero_id\":92,\"item_0\":79,\"item_1\":180,\"item_2\":36,\"item_3\":90,\"item_4\":0,\"item_5\":0,\"kills\":3,\"deaths\":3,\"assists\":17,\"leaver_status\":0,\"gold\":2200,\"last_hits\":31,\"denies\":1,\"gold_per_min\":298,\"xp_per_min\":336,\"gold_spent\":9388,\"hero_damage\":8401,\"tower_damage\":1073,\"hero_healing\":867,\"level\":15,\"ability_upgrades\":[{\"ability\":5482,\"time\":213,\"level\":1},{\"ability\":5481,\"time\":310,\"level\":2},{\"ability\":5481,\"time\":352,\"level\":3},{\"ability\":5480,\"time\":440,\"level\":4},{\"ability\":5481,\"time\":611,\"level\":5},{\"ability\":5480,\"time\":693,\"level\":6},{\"ability\":5481,\"time\":881,\"level\":7},{\"ability\":5483,\"time\":1098,\"level\":8},{\"ability\":5480,\"time\":1266,\"level\":9},{\"ability\":5480,\"time\":1489,\"level\":10},{\"ability\":5483,\"time\":1553,\"level\":11},{\"ability\":5482,\"time\":1919,\"level\":12},{\"ability\":5482,\"time\":1990,\"level\":13},{\"ability\":5482,\"time\":2146,\"level\":14},{\"ability\":5002,\"time\":2476,\"level\":15}]},{\"account_id\":4294967295,\"player_slot\":130,\"hero_id\":53,\"item_0\":152,\"item_1\":98,\"item_2\":96,\"item_3\":63,\"item_4\":65,\"item_5\":88,\"kills\":7,\"deaths\":6,\"assists\":17,\"leaver_status\":0,\"gold\":3534,\"last_hits\":277,\"denies\":5,\"gold_per_min\":642,\"xp_per_min\":751,\"gold_spent\":16900,\"hero_damage\":12718,\"tower_damage\":1597,\"hero_healing\":0,\"level\":23,\"ability_upgrades\":[{\"ability\":5247,\"time\":169,\"level\":1},{\"ability\":5246,\"time\":285,\"level\":2},{\"ability\":5247,\"time\":336,\"level\":3},{\"ability\":5245,\"time\":479,\"level\":4},{\"ability\":5247,\"time\":531,\"level\":5},{\"ability\":5248,\"time\":641,\"level\":6},{\"ability\":5247,\"time\":685,\"level\":7},{\"ability\":5246,\"time\":746,\"level\":8},{\"ability\":5246,\"time\":870,\"level\":9},{\"ability\":5246,\"time\":1080,\"level\":10},{\"ability\":5248,\"time\":1103,\"level\":11},{\"ability\":5245,\"time\":1328,\"level\":12},{\"ability\":5245,\"time\":1370,\"level\":13},{\"ability\":5245,\"time\":1502,\"level\":14},{\"ability\":5002,\"time\":1614,\"level\":15},{\"ability\":5248,\"time\":1890,\"level\":16},{\"ability\":5002,\"time\":1943,\"level\":17},{\"ability\":5002,\"time\":2180,\"level\":18},{\"ability\":5002,\"time\":2299,\"level\":19},{\"ability\":5002,\"time\":2386,\"level\":20},{\"ability\":5002,\"time\":2396,\"level\":21},{\"ability\":5002,\"time\":2458,\"level\":22},{\"ability\":5002,\"time\":2462,\"level\":23}]},{\"account_id\":4294967295,\"player_slot\":131,\"hero_id\":31,\"item_0\":79,\"item_1\":214,\"item_2\":108,\"item_3\":0,\"item_4\":46,\"item_5\":0,\"kills\":7,\"deaths\":6,\"assists\":6,\"leaver_status\":0,\"gold\":1791,\"last_hits\":63,\"denies\":2,\"gold_per_min\":299,\"xp_per_min\":430,\"gold_spent\":8716,\"hero_damage\":9625,\"tower_damage\":791,\"hero_healing\":569,\"level\":17,\"ability_upgrades\":[{\"ability\":5134,\"time\":165,\"level\":1},{\"ability\":5135,\"time\":318,\"level\":2},{\"ability\":5134,\"time\":338,\"level\":3},{\"ability\":5136,\"time\":394,\"level\":4},{\"ability\":5134,\"time\":506,\"level\":5},{\"ability\":5137,\"time\":569,\"level\":6},{\"ability\":5136,\"time\":604,\"level\":7},{\"ability\":5134,\"time\":680,\"level\":8},{\"ability\":5136,\"time\":857,\"level\":9},{\"ability\":5136,\"time\":1023,\"level\":10},{\"ability\":5137,\"time\":1043,\"level\":11},{\"ability\":5135,\"time\":1226,\"level\":12},{\"ability\":5135,\"time\":1319,\"level\":13},{\"ability\":5135,\"time\":1501,\"level\":14},{\"ability\":5002,\"time\":1749,\"level\":15},{\"ability\":5137,\"time\":2116,\"level\":16},{\"ability\":5002,\"time\":2143,\"level\":17}]},{\"account_id\":110928105,\"player_slot\":132,\"hero_id\":49,\"item_0\":152,\"item_1\":154,\"item_2\":112,\"item_3\":36,\"item_4\":63,\"item_5\":46,\"kills\":17,\"deaths\":4,\"assists\":15,\"leaver_status\":0,\"gold\":5225,\"last_hits\":153,\"denies\":9,\"gold_per_min\":550,\"xp_per_min\":856,\"gold_spent\":16389,\"hero_damage\":24287,\"tower_damage\":2961,\"hero_healing\":0,\"level\":25,\"ability_upgrades\":[{\"ability\":5228,\"time\":170,\"level\":1},{\"ability\":5226,\"time\":260,\"level\":2},{\"ability\":5226,\"time\":300,\"level\":3},{\"ability\":5228,\"time\":358,\"level\":4},{\"ability\":5228,\"time\":431,\"level\":5},{\"ability\":5229,\"time\":471,\"level\":6},{\"ability\":5227,\"time\":525,\"level\":7},{\"ability\":5226,\"time\":555,\"level\":8},{\"ability\":5227,\"time\":668,\"level\":9},{\"ability\":5228,\"time\":697,\"level\":10},{\"ability\":5229,\"time\":786,\"level\":11},{\"ability\":5226,\"time\":1125,\"level\":12},{\"ability\":5227,\"time\":1182,\"level\":13},{\"ability\":5227,\"time\":1248,\"level\":14},{\"ability\":5002,\"time\":1296,\"level\":15},{\"ability\":5229,\"time\":1368,\"level\":16},{\"ability\":5002,\"time\":1577,\"level\":17},{\"ability\":5002,\"time\":1712,\"level\":18},{\"ability\":5002,\"time\":1859,\"level\":19},{\"ability\":5002,\"time\":1936,\"level\":20},{\"ability\":5002,\"time\":2111,\"level\":21},{\"ability\":5002,\"time\":2276,\"level\":22},{\"ability\":5002,\"time\":2331,\"level\":23},{\"ability\":5002,\"time\":2424,\"level\":24},{\"ability\":5002,\"time\":2424,\"level\":25}]}],\"radiant_win\":false,\"duration\":2293,\"start_time\":1381906236,\"match_id\":346805844,\"match_seq_num\":316035716,\"tower_status_radiant\":4,\"tower_status_dire\":1846,\"barracks_status_radiant\":3,\"barracks_status_dire\":63,\"cluster\":151,\"first_blood_time\":112,\"lobby_type\":0,\"human_players\":10,\"leagueid\":0,\"positive_votes\":0,\"negative_votes\":0,\"game_mode\":1}}");
        JSONArray players = result.optJSONArray("players");
        Player player; String data = null;
        for (int i = 0; i < players.length(); i++) {
            player = new Player(players.getJSONObject(i));
            data += player.getFormattedHero();
        }
        display.setText(data);

假设我的项目中有这个代码。我想知道这段代码有什么问题。如果有人能帮助我,我会很高兴。单击here以查看格式化的json字符串。

3 个答案:

答案 0 :(得分:2)

当前json字符串players中的

JSONArray,它位于result JSONObject内,因此您需要首先从主result获取JSONObject JSONObject然后获取players JSONArray来自:

JSONObject result = new JSONObject(<JSON_STRING_HERE>);
 // get result JSONObject
JSONObject jsonobj_result = result.optJSONObject("result");
 // get players JSONArray
JSONArray players = jsonobj_result.optJSONArray("players");
Player player; String data = null;
for (int i = 0; i < players.length(); i++) {
    player = new Player(players.getJSONObject(i));
    data += player.getFormattedHero();
}

答案 1 :(得分:0)

      JSONObject result;
                URL url = new URL(
                        "");
                URLConnection openConnection = url.openConnection();
                openConnection.connect();
                InputStream inputStream = openConnection.getInputStream();
                String inputStream2String = inputStream2String(inputStream);
                result = new JSONObject(inputStream2String);
                // here is my add code
                // you can look the all names in the thi jsonObject
                JSONArray names = result.names();// no hava players name in
                                                    // here; so
                String string = result.getString("result");
                result = new JSONObject(string);
                // my add code end
                // look names again
                names = result.names();// hava players name so get
                                        // optJSONArray for players
                JSONArray players = result.optJSONArray("players");// getData   is ok

答案 2 :(得分:0)

String data = null;

替换为

StringBuffer data = new StringBuffer();

可能会更好。