0

I am new in android JSON parsing android. Below I mentioned my JSON value, please give the solution to how to parse the JSON value, thanks in advance my JSON Data:

Here I have category, categorybook, subcategory inside have subbook. I want to parse all the value and store it into sqlite with separate. Like category book and sucategory book are stored seperate column in android sqlite table:

[
  "category": {
    "id": "1",
    "name": "Education",
    "categorybooks": [],

    "subcategory": [

      {
        "id": "1",
        "name": "tamil",
        "subcategorybooks": []
      },
      {
        "id": "2",
        "name": "english",
        "subcategorybooks": []
      },
      {
        "id": "5",
        "name": "maths",
        "subcategorybooks": []
      },
      {
        "id": "6",
        "name": "science,
        "subcategorybooks": []
      },
      {
        "id": "7",
        "name": "social",
        "subcategorybooks": []
      }
  }
]

Here i have category 2 and its sub category books:

[
  "category": {

    "id": "2",
    "name": "sports",
    "categorybooks": [
      {
        "id": "4",
        "name": "football",
        },
      {
        "id": "5",
        "name": "cricket",
      }
     ],

     "subcategory": []
   }
]
diogojme
  • 2,309
  • 1
  • 19
  • 25
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
  • Have you looked into any tutorials before posting? There are literally plenty of them online. http://www.appifiedtech.net/2015/06/19/android-json-parsing-with-sqlite-example/ – denvercoder9 Dec 10 '15 at 12:53

4 Answers4

1

First of all, you will need the VolleyLibrary. Then, you will need the GSON library. When creating GSON Request, you put the Object you want to get deserialized automatically from JSON, so you wont have to parse it manualy.

Second thing, you would want to create a db class that extends SQLiteHelper. When you create database, add the methods for adding, update and removing a row for every table that you create. Below are the links that you need.

http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

https://medium.com/android-news/android-networking-i-okhttp-volley-and-gson-72004efff196#.wp5lstsfv

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

Vulovic Vukasin
  • 1,540
  • 2
  • 21
  • 30
1

I will assume you already have the content of your json, if not you can use any Http Connection to get the content.

You can use Gson to parse your json content, first of all, you need to create your models based on your Json:

//note: Gson identify the attributes name as the same of from your
//json, if not you have to put an annotation above each attribute of 
//your class:

public class Category(){
  int id;
  String name;
  List<CategoryBook> categoryBooks;
}

public class CategoryBook(){
  int id;
  String name;
}

public class SubCategory(){
 //Based on your json I will assume the attribute of this class
 // is a model that have a list of an object that have an id and a name 
 //and a list of subcategorybooks
}

public class SubCategoryBooks(){
  //class attributes that you not mentioned in your json
}

Then you just need to parse your object from content using gson, if you have some doubts about how to parse using Gson follow this tutorial

For insert into database separate, you already have your objects with getters and setters, for example you can get the List<CategoryBook> from your CategoryModel, then your can insert this list inside a separate table of your database.

diogojme
  • 2,309
  • 1
  • 19
  • 25
0
Hope It's Help you

JSONObject jsonObject = new JSONObject();
jsonObject.put("category",loaddata());

public JSONObject loaddata(){

JSONObject jsonObject = new JSONObject();
jsonObject.put("id","1");
jsonObject.put("name","Education");
jsonObject.put("categorybooks",addcategorybooks());
jsonObject.put("subcategory","addsubcategory()");

return jsonObject;

}

public JSONArray addcategorybooks(){

    JSONArray JsonArray = new JSONArray();
    JSONObject jObj = new JSONObject();
    jObj.put("id",  "1");
    jObj.put("name", "Foot Ball");
    JsonArray.put(jObj);
    return JsonArray;
}

public JSONArray addsubcategory(){

    JSONArray JsonArray = new JSONArray();
    JSONObject jObj = new JSONObject();
    jObj.put("id",  "1");
    jObj.put("name", "Foot Ball");
    jObj.put("subcategorybooks", addsubcategorybooks());
    JsonArray.put(jObj);
    return JsonArray;
}

public JSONArray addsubcategorybooks(){

    JSONArray JsonArray = new JSONArray();
    JSONObject jObj = new JSONObject();
    jObj.put("id",  "1");
    jObj.put("name", "Foot Ball");
    JsonArray.put(jObj);
    return JsonArray;
}
PLP
  • 714
  • 3
  • 13
  • "Code-only answers on SO are rarely desirable. Please consider adding an explanation as to what it is and why it should work for the OP." – denvercoder9 Dec 10 '15 at 12:50
0

If i understood you correctly, the main problem is to get java model object from json.

There are several ways to do it. The most common are:

  1. Using built-in tools to operate with Json. In this case you get org.json.JSONObject and get values using it's methods. See details f.e. here

  2. Using gson library. In this case you create model class and you can fill the object with one code line. smth like Category category = gson.fromJson(yourJsonString, Category.class); See details in official tutorial

Silwester
  • 418
  • 5
  • 12