0

http://pastie.org/8269975 . If you look at my code, i don't know how to access the individual fields like account_id, hero_id, and etc. I have this code.

JSONObject match = getMatchDetails("281699681");

if i want to access those individual fields like account_id, then this code is not working.

int a = match.result.players[0].account_id;

please help me and if you want to look at the JSON script, here's the link https://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/?match_id=281699681&key=8F48DE9EBF9C9EA121A6A9EF2E30B7C7

3 Answers 3

1

JSON uses JavaScript Objects, not Java objects and hence you cannot directly access members as if they were Java objects. You need something like GSON to convert between JSON representation and Java object representation.

Sign up to request clarification or add additional context in comments.

Comments

0

Try This method to iterate any json object dynamically.

private void parseJson(JSONObject data) {

        if (data != null) {
            Iterator<String> it = data.keys();
            while (it.hasNext()) {
                String key = it.next();

                try {
                    if (data.get(key) instanceof JSONArray) {
                        JSONArray arry = data.getJSONArray(key);
                        int size = arry.length();
                        for (int i = 0; i < size; i++) {
                            parseJson(arry.getJSONObject(i));
                        }
                    } else if (data.get(key) instanceof JSONObject) {
                        parseJson(data.getJSONObject(key));
                    } else {
                        System.out.println("" + key + " : " + data.optString(key));
                    }
                } catch (Throwable e) {
                    System.out.println("" + key + " : " + data.optString(key));
                    e.printStackTrace();

                }
            }
        }
    }

3 Comments

err, i do not get it sir. will that parseJson() function will give me the individual fields of the player[0] object ?
had you try this function? it will print all keys and value.
uhm sir, i need to know the individual values, because i will associate it to some pictures and other media.
0

First of all, try replacing the line,

    String MatchDetailsURL = "https://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/?match_id"+ matchID + "=&key=" + SteamWebAPIKey;

with,

    String MatchDetailsURL = "https://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/?match_id="+ matchID + "&key=" + SteamWebAPIKey;

in your code and try.

Bcs, instead of giving match_id="+ matchID + "&key you have given match_id"+ matchID + "=&key..So, the URL itself is wrong and it returns a bad request..

1 Comment

it helped me, but it doesn't answer my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.