-1

I'm currently making a bible verse app for android, and the problem I am having might be common, but I find it difficult to implement since I'm a beginner.

How will I get each information in a string from the json data sampled below ?

{
    "book": "1 John",
    "chapters": [{
        "chapter": "1",
        "verses": [{
            "1": "That which was from the beginning, which we have heard, which we have seen with our eyes, which we have looked upon, and our hands have handled, of the Word of life;"
        }, {
            "2": "(For the life was manifested, and we have seen it, and bear witness, and shew unto you that eternal life, which was with the Father, and was manifested unto us;)"
        }]
    }]

If I were to get each data so that I can use it as a string or a list, should I use a code like the following ? How can I change the code so that it can get the data from the json ? I would love to hear from you!

JSONObject obj = new JSONObject(script);
JSONArray chapters = obj.getJSONArray("chapters");

ArrayList < HashMap < String, String >> formList = new ArrayList < HashMap < String, String >> ();
HashMap < String, String > m_li;

for (int i = 0; i < chapters.length(); i++) {
    JSONObject jo_inside = chapters.getJSONObject(i);
    String formula_value = jo_inside.getString("chapters");
    String url_value = jo_inside.getString("verse");
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Jennifer
  • 1,822
  • 2
  • 20
  • 45
  • Take a look at Gson library, with it you just need to create model classes to store your data, it makes it so much easy to do. – Nanoc Dec 01 '15 at 16:40
  • 1
    Possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Selvin Dec 01 '15 at 16:41
  • What exactly are you asking? You are close with what you have posted, but `jo_inside.getString("chapters)"` won't work because there is no `chapters` string. Same for `"verse"` – OneCricketeer Dec 01 '15 at 16:59
  • Can you post your full json i can show you a easy way to do it by making class or modals of the json. – Pankaj Dec 01 '15 at 17:14

1 Answers1

1

Better representation of data would be like this.

{
  "book": "1 John",
  "chapters": [
    {
      "chapter": "1",
      "verses": [
        { 
          "1": "That which was from the beginning, which we have heard, which we have seen with our eyes, which we have looked upon, and our hands have handled, of the Word of life;"
        },
        { 
          "2": "(For the life was manifested, and we have seen it, and bear witness, and shew unto you that eternal life, which was with the Father, and was manifested unto us);"
        }
      ]
    }
  ]
}

As you can see we have two level of list which are represented by [ ] in JSON. I have also removed unnecessary complex JSON object.

JSONObject obj = new JSONObject(script);
JSONArray chaptersList  = obj.getJSONArray("chapters");

ArrayList<HashMap<String, String>> chapterHash = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i < chaptersList.length(); i++) {
        JSONObject chapter = chaptersList.getJSONObject(i);
        String chapterId = chapter.getString("chapter");
        chapterHash.add(new HashMap<String, String>());
        JSONArray versesList = chapter.getJSONArray("verses");
        for(int j=0;j < versesList.length();j++){
            JSONObject verse = versesList.getJSONObject(j);
            for(int k = 0; k<verse.names().length(); k++){
                String verseKey = verse.names().getString(k);
                String verseContent = verse.getString(verseKey);
                chapterHash.get(i).put(verseKey,verseContent);
            }
        }
    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Ahsan
  • 121
  • 4