0

This is my JSON file:

[
  {"frag1":
        [
           {"Title":"hello this is first fragment"},
           {"SubTitle":"Hello"},
           {"photo":"http://edition.cnn.com/2016/08/01/sport/refugee-runners-kenya-mckenzie/index.html"}
        ]},

  {"frag2":
        [
          {"Title":"hello this is second fragment"},
          {"SubTitle":"Hi"},
          {"About":"this is the about"}
        ]}
] 

I am using this method to read the JSON file from assets folder:

public String loadJSONFromAsset() {
        String json;
        try {
            InputStream is = getApplication().getAssets().open("json.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }

I want to split this JSON into 2 parts and put it in lists, so the list1[0] will contain the "Title", list1[1] will contain the "SubTitle" and list1[2] will contain the "photo". And of course the same about list2 with the JSON second part.

Update:

So, this is what I came up with, please advise for a better way to accomplish this, I am looking for a more general way, I mean without writing "Title" like here: listFrag1.add(jsonArrayFrag1.getJSONObject(0).getString("Title")); lets say I don't know what are object's names, is it possible?

public void handleJSON() {

        ArrayList listFrag1Frag2 = new ArrayList();
        try {
            JSONArray fullJson = new JSONArray(loadJSONFromAsset());
            for (int i = 0; i < fullJson.length(); i++) {
                listFrag1Frag2.add(fullJson.getString(i));  //splits the original json into 2 objects, frag1 and frag2
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            //getting first part of json: frag1: title, subtitle and photo
            ArrayList listFrag1 = new ArrayList();
            JSONObject objectFrag1 = new JSONObject(listFrag1Frag2.get(0).toString());
            JSONArray jsonArrayFrag1 = objectFrag1.getJSONArray("frag1");
            listFrag1.add(jsonArrayFrag1.getJSONObject(0).getString("Title"));
            listFrag1.add(jsonArrayFrag1.getJSONObject(1).getString("SubTitle"));
            listFrag1.add(jsonArrayFrag1.getJSONObject(2).getString("Photo"));

            //getting second part of json: frag2: title, subtitle and about
            ArrayList listFrag2 = new ArrayList();
            JSONObject objectFrag2 = new JSONObject(listFrag1Frag2.get(1).toString());
            JSONArray jsonArrayFrag2 = objectFrag2.getJSONArray("frag2");
            listFrag2.add(jsonArrayFrag2.getJSONObject(0).getString("Title"));
            listFrag2.add(jsonArrayFrag2.getJSONObject(1).getString("SubTitle"));
            listFrag2.add(jsonArrayFrag2.getJSONObject(2).getString("About"));

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

0 Answers0