0

I am trying to read this JSON and get the value for each key. The JSON looks like this in the url.

{"gameList":[{"gameId":2889909885,"mapId":12,"gameMode":"ARAM","gameType":"MATCHED_GAME","gameQueueConfigId":450,"participants":[{"teamId":100,"spell1Id":4,"spell2Id":14,"championId":103,"profileIconId":1665,"summonerName":"suprise motherfk","bot":false},{"teamId":100,"spell1Id":32,"spell2Id":14,"championId":111,"profileIconId":539,"summonerName":"Celebi Master","bot":false},{"teamId":100,"spell1Id":4,"spell2Id":7,"championId":163,"profileIconId":577,"summonerName":"Sontz","bot":false},{"teamId":100,"spell1Id":4,"spell2Id":32,"championId":59,"profileIconId":3566,"summonerName":"Milhao","bot":false},{"teamId":100,"spell1Id":3,"spell2Id":4,"championId":202,"profileIconId":3764,"summonerName":"Fistacles","bot":false},{"teamId":200,"spell1Id":32,"spell2Id":4,"championId":154,"profileIconId":539,"summonerName":"Dante Highwind","bot":false},{"teamId":200,"spell1Id":4,"spell2Id":7,"championId":134,"profileIconId":3587,"summonerName":"Get Derived","bot":false},{"teamId":200,"spell1Id":32,"spell2Id":4,"championId":14,"profileIconId":3715,"summonerName":"Hestory","bot":false},{"teamId":200,"spell1Id":4,"spell2Id":7,"championId":126,"profileIconId":3541,"summonerName":"Jayjang","bot":false},{"teamId":200,"spell1Id":14,"spell2Id":4,"championId":157,"profileIconId":578,"summonerName":"Kledy McKledFace","bot":false}],"observers":{"encryptionKey":"nRC9PwcmfeYTQg6F2cWjE0//wCgOI40z"},"platformId":"NA1","bannedChampions":[],"gameStartTime":1539909193767,"gameLength":46},{"gameId":2889900079,"mapId":12,"gameMode":"ARAM","gameType":"MATCHED_GAME","gameQueueConfigId":450,"participants":[{"teamId":100,"spell1Id":4,"spell2Id":32,"championId":266,"profileIconId":3186,"summonerName":"meScott","bot":false},{"teamId":100,"spell1Id":32,"spell2Id":4,"championId":12,"profileIconId":3552,"summonerName":"chogs08","bot":false},{"teamId":100,"spell1Id":21,"spell2Id":4,"championId":67,"profileIconId":3551,"summonerName":"Constantinople","bot":false},{"teamId":100,"spell1Id":7,"spell2Id":4,"championId":96,"profileIconId":3717,"summonerName":"tanjo3","bot":false},{"teamId":100,"spell1Id":3,"spell2Id":4,"championId":84,"profileIconId":3632,"summonerName":"xR4YZ0Rx","bot":false},{"teamId":200,"spell1Id":4,"spell2Id":32,"championId":3,"profileIconId":3102,"summonerName":"RAT IG","bot":false},{"teamId":200,"spell1Id":4,"spell2Id":32,"championId":76,"profileIconId":3614,"summonerName":"IM ALIVE OH YEAH","bot":false},{"teamId":200,"spell1Id":4,"spell2Id":3,"championId":26,"profileIconId":3587,"summonerName":"donuts please","bot":false},{"teamId":200,"spell1Id":21,"spell2Id":4,"championId":38,"profileIconId":3778,"summonerName":"Smelloscope","bot":false},{"teamId":200,"spell1Id":21,"spell2Id":4,"championId":15,"profileIconId":3379,"summonerName":"xDingwithsalad","bot":false}],"observers":{"encryptionKey":"XaGeGBdnfK5MrJpZLDwYpptCBRvD1uwD"},"platformId":"NA1","bannedChampions":[],"gameStartTime":1539908957553,"gameLength":282},{"gameId":2889909927,"mapId":12,"gameMode":"ARAM","gameType":"MATCHED_GAME","gameQueueConfigId":450,"participants":[{"teamId":100,"spell1Id":32,"spell2Id":4,"championId":9,"profileIconId":3614,"summonerName":"Trickster Canvas","bot":false},{"teamId":100,"spell1Id":14,"spell2Id":4,"championId":64,"profileIconId":3009,"summonerName":"Fernue","bot":false},{"teamId":100,"spell1Id":7,"spell2Id":4,"championId":432,"profileIconId":3781,"summonerName":"Jack Daniels","bot":false},{"teamId":100,"spell1Id":32,"spell2Id":4,"championId":41,"profileIconId":940,"summonerName":"SwoopDogg","bot":false},{"teamId":100,"spell1Id":32,"spell2Id":4,"championId":30,"profileIconId":3440,"summonerName":"Valeth","bot":false},{"teamId":200,"spell1Id":21,"spell2Id":4,"championId":142,"profileIconId":3355,"summonerName":"MaxtheShady","bot":false},{"teamId":200,"spell1Id":21,"spell2Id":4,"championId":99,"profileIconId":745,"summonerName":"FEARedound","bot":false},{"teamId":200,"spell1Id":4,"spell2Id":7,"championId":101,"profileIconId":3072,"summonerName":"Tataki","bot":false},{"teamId":200,"spell1Id":4,"spell2Id":6,"championId":21,"profileIconId":3776,"summonerName":"IED Enilex","bot":false},{"teamId":200,"spell1Id":32,"spell2Id":4,"championId":412,"profileIconId":3147,"summonerName":"Juvia is Life","bot":false}],"observers":{"encryptionKey":"CF6RMjlV2JHR3LHwARX314ZgrShLaOGB"},"platformId":"NA1","bannedChampions":[],"gameStartTime":1539909218114,"gameLength":21},{"gameId":2889897590,"mapId":12,"gameMode":"ARAM","gameType":"MATCHED_GAME","gameQueueConfigId":450,"participants":[{"teamId":100,"spell1Id":4,"spell2Id":13,"championId":16,"profileIconId":603,"summonerName":"Lionhead","bot":false},{"teamId":100,"spell1Id":32,"spell2Id":4,"championId":12,"profileIconId":607,"summonerName":"Habebe","bot":false},{"teamId":100,"spell1Id":4,"spell2Id":7,"championId":13,"profileIconId":3461,"summonerName":"MathiasBlint","bot":false},{"teamId":100,"spell1Id":32,"spell2Id":4,"championId":22,"profileIconId":3156,"summonerName":"Old Dirty Juice","bot":false},{"teamId":100,"spell1Id":4,"spell2Id":3,"championId":74,"profileIconId":3167,"summonerName":"Hamo Hamo","bot":false},{"teamId":200,"spell1Id":7,"spell2Id":4,"championId":161,"profileIconId":931,"summonerName":"AD Paul","bot":false},{"teamId":200,"spell1Id":4,"spell2Id":13,"championId":31,"profileIconId":508,"summonerName":"LuckyClicker","bot":false},{"teamId":200,"spell1Id":4,"spell2Id":32,"championId":86,"profileIconId":691,"summonerName":"Charge Runner","bot":false},{"teamId":200,"spell1Id":32,"spell2Id":4,"championId":24,"profileIconId":3762,"summonerName":"Cutest Timo NA","bot":false},{"teamId":200,"spell1Id":4,"spell2Id":7,"championId":67,"profileIconId":3232,"summonerName":"Hoiyuen","bot":false}],"observers":{"encryptionKey":"oDkdEttv1Egnm+l4O9FiT7E9wA9XfgoH"},"platformId":"NA1","bannedChampions":[],"gameStartTime":1539909116804,"gameLength":123},{"gameId":2889889051,"mapId":12,"gameMode":"ARAM","gameType":"MATCHED_GAME","gameQueueConfigId":450,"participants":[{"teamId":100,"spell1Id":4,"spell2Id":13,"championId":202,"profileIconId":523,"summonerName":"16500211","bot":false},{"teamId":100,"spell1Id":4,"spell2Id":21,"championId":115,"profileIconId":3366,"summonerName":"FIippers","bot":false},{"teamId":100,"spell1Id":32,"spell2Id":4,"championId":72,"profileIconId":1298,"summonerName":"PaaTy","bot":false},{"teamId":100,"spell1Id":4,"spell2Id":3,"championId":74,"profileIconId":691,"summonerName":"TSOL SI EMAG EHT","bot":false},{"teamId":100,"spell1Id":4,"spell2Id":32,"championId":44,"profileIconId":3373,"summonerName":"Darkned","bot":false},{"teamId":200,"spell1Id":32,"spell2Id":4,"championId":41,"profileIconId":3499,"summonerName":"jugulaur","bot":false},{"teamId":200,"spell1Id":7,"spell2Id":4,"championId":67,"profileIconId":3777,"summonerName":"Kàrmy","bot":false},{"teamId":200,"spell1Id":13,"spell2Id":4,"championId":45,"profileIconId":3621,"summonerName":"Blunt Hits","bot":false},{"teamId":200,"spell1Id":7,"spell2Id":4,"championId":104,"profileIconId":786,"summonerName":"Buzz","bot":false},{"teamId":200,"spell1Id":7,"spell2Id":4,"championId":43,"profileIconId":1665,"summonerName":"PanPanMJ","bot":false}],"observers":{"encryptionKey":"nq3p1TBm9GLexdtxQcx9Dw522mfISRu2"},"platformId":"NA1","bannedChampions":[],"gameStartTime":1539908962887,"gameLength":276}],"clientRefreshInterval":300}

It is a very big line so to make it easier to view here is an image on how it looks like on my browser.

https://i.stack.imgur.com/sAliR.jpg

Using

System.out.println(json.get("gameList"));

results in everything after

{"gameList":[{

What I want to do is know how to pick individual keys within it. For example if I want the gameId or lets say a summonerName from one of the games. If I want the summoner "surprise motherfk" how can I get to him from the JSON.

Im sorry if this is an easy question I couldn't figure it out.

7
  • Unclear what JSON library you're using, but try json.get("gameList[0]"), or otherwise, you're already getting an array, and you need to loop over it Oct 19, 2018 at 1:11
  • @cricket_007 I imported this import org.json.JSONObject;
    – yibs
    Oct 19, 2018 at 1:13
  • There should be a different get method for getting an array stleary.github.io/JSON-java/org/json/… Oct 19, 2018 at 1:17
  • @cricket_007 so the best way to do this is use JSONarray? Im still a beginner programmer sorry for all questions
    – yibs
    Oct 19, 2018 at 1:19
  • It's the only way. So does that make it the "best"? Oct 19, 2018 at 1:22

1 Answer 1

0

Here is how i map my JSON responses

String result = getJsonResponse();

    try {
        JSONArray jsonResponse = new JSONArray(result);

        // Step through each of the Items returned
        for (int i = 0; i <jsonResponse.length(); i++)
        {
            JSONObject jsonItem = jsonResponse.getJSONObject(i);

            Item item = new Item();

            item .setIndex(jsonItem.getInt("Index"));
            item .setName(jsonItem.getString("Name"));
            item .setActive(jsonItem.getBoolean("Active"));
            item .setExists(jsonItem.getBoolean("Exists"));
            item .setShouldExecuteSchedule(jsonItem.getBoolean("Execute"));

            hardware.getItems().add(item);
        }

    } catch (JSONException e) {
        e.printStackTrace();
        passed = false;
    }
1
  • Unrelated: "get response code" doesn't sound like it should return string content, rather a numeric code Oct 19, 2018 at 1:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.