Parse Json from Json array in Android

579 views Asked by At

I have the following JSON and I would like to parse all of the "vcsapat" and "hcsapat" and all of the datas from the first rows.

Json is here:Link

I tried with the following code, but I only have exceptions

JSONArray JSonAdatok = null;
JSonAdatok = jObject.getJSONArray("vcsapat");

for (int i = 0; i < JSonAdatok.length(); i++) {
    JSONObject jo = null;
    try {
        jo = JSonAdatok.getJSONObject(i);
        System.out.print("\n"+jo);
        JSONObject kezdojatekosok = jo.getJSONObject("kezdo");
        System.out.print("\n"+kezdojatekosok);
        for (int j = 0; j < kezdojatekosok.length(); j++) {
            JSONObject egyjjson = kezdojatekosok;

And the Exception is the following:

at org.json.JSON.typeMismatch(JSON.java:100)
org.json.JSONObject.getJSONArray(JSONObject.java:588)
1

There are 1 answers

0
galex On BEST ANSWER

Your JSON contains first an Object which starts with "{" so your whole JSON string represents an JSONObject. Then after, get inside it the object "hforma" then the JSONArray of forma, and then you can iterate on them to get on each the hcsapat and vcsapat attributes as strings.

Something like this:

JSONObject complete = new JSONObject(WHOLE_JSON_AS_STRING);
JSONObject hforma = complete.getJSONObject("hforma");
JSONArray forma = hforma.getJSONArray("forma");

for (int i = 0; i < forma.length(); i++) {
   JSONObject formaData = forma.getJSONObject(i);
   String hcsapat = formaData.getString("hcsapat");
   String vcsapat = formaData.getString("vcsapat");
}

I did not test that code, it is just an example that follows the structure of your JSON.