Luke Batley
Luke Batley

Reputation: 2402

Android create a JSON array of JSON Objects

hi does anyone know how to create a Array that contains objects that in each objects contain several objects? i just can't seem to get my head round it

the structure should look like this

Array{[object]{subobject,subobject}
      [object]{subobject,subobject}
     }

heres what i have so far

JSONObject obj = new JSONObject(json2);    
            JSONObject objData = obj.getJSONObject("data");
            fixturesArray = objData.getJSONArray("fixtures");

            JSONArray FixArray = new JSONArray();


            for(int t = 0; t < fixturesArray.length(); t++){
            JSONObject fixObj = fixturesArray.getJSONObject(t);
            String Matchdate = fixObj.getString("matchdate");
            JSONObject DateObj = DateObj.put(Matchdate, DateObj);

heres my JSON essentially what i have if is a feed of fixtures i need to order them in to arrays of dates

{
    "code":200,
    "error":null,
    "data":{
        "fixtures":[
            {
                "kickoff":"15:00:00",
                "matchdate":"2012-07-28",
                "homescore":null,
                "awayscore":null,
                "attendance":null,
                "homepens":null,
                "awaypens":null,
                "division_id":"5059",
                "division":"Testing 1",
                "comp":"LGE",
                "location":null,
                "fixture_note":null,
                "hometeam_id":"64930",
                "hometeam":"Team 1",
                "awayteam_id":"64931",
                "awayteam":"Team 2"
            }, {
                "kickoff":"15:00:00",
                "matchdate":"2012-07-28",
                "homescore":null,
                "awayscore":null,
                "attendance":null,
                "homepens":null,
                "awaypens":null,
                "division_id":"5059",
                "division":"Testing 1",
                "comp":"LGE",
                "location":null,
                "fixture_note":null,
                "hometeam_id":"64930",
                "hometeam":"Team 1",
                "awayteam_id":"64931",
                "awayteam":"Team 2"
            }
        ]
    }
}

Upvotes: 1

Views: 14784

Answers (4)

Nirali
Nirali

Reputation: 13825

JSON String

{
"result": "success",
"countryCodeList":
[
  {"countryCode":"00","countryName":"World Wide"},
  {"countryCode":"kr","countryName":"Korea"}
] 
}

Here below I am fetching country details

JSONObject json = new JSONObject(jsonstring);
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);

JSONArray valArray1 = valArray.getJSONArray(1);

valArray1.toString().replace("[", "");
valArray1.toString().replace("]", "");

int len = valArray1.length();

for (int i = 0; i < valArray1.length(); i++) {

 Country country = new Country();
 JSONObject arr = valArray1.getJSONObject(i);
 country.setCountryCode(arr.getString("countryCode"));                        
 country.setCountryName(arr.getString("countryName"));
 arrCountries.add(country);
}

Upvotes: 2

rallat
rallat

Reputation: 1305

What I would suggest to do is to use JackSON JSON Parser library http://jackson.codehaus.org/ Then you can create a Class with the same fields as the JSON son the mapping from JSON To class will be direct. So once you have all the items from JSON into a List of class you can order by dates or manipulate data as you want. Imagine that src is a String containing the JSON text. With JackSON lib you just need to do this.

ObjectMapper mapper = new ObjectMapper();

List<Fixture> result = mapper.readValue(src, new TypeReference<List<Fixture>>() { });

Upvotes: 1

Michiel
Michiel

Reputation: 270

Here are two pieces of JSON which fit your description which is "Array that contains objects that in each objects contain several objects". The first method uses Arrays inside Objects. The other one uses Objects in Objects.

Method 1

[ { "name" : "first object in array" , "inner array" : [ { <object> } , { <object> } ] }
 , { "name" : "second object in array" , "inner array" : [ { <object> } , { <object> } ] } ]

To parse the above you need two nested for loops (or something recursive).

Method 2

[ { "name" : "first object in array" , "first inner object" : { <object> } , "second inner object" : { <object> } } , <etc.> ] } ]

The second method can be parsed with a single for loop because you know in advance the number of inner objects to expect.

Upvotes: 0

Litus
Litus

Reputation: 632

Do you mean that?:

JSONObject obj = new JSONObject();
obj.put("x", "1");
JSONObject parent_object = new JSONObject();
parent_object.put("child", obj);
JSONArray array = new JSONArray(parent_object.toString());

Upvotes: 5

Related Questions