0

I just tried to get values that are stored in my JSON file and save it into sqlite database:

This is my JSON file:

{
  "list": {
    "meta": {
      "count": 132, 
      "start": 0, 
      "type": "resource-list"
    }, 
    "resources": [
      {
        "resource": {
          "classname": "Quote", 
          "fields": {
            "date": "2017-03-16", 
            "price": 3.6720000000000002, 
            "type": "currency", 
            "symbol": "AED=X"
          }
        }
      }, 
      {
        "resource": {
          "classname": "Quote", 
          "fields": {
            "date": "2017-03-16", 
            "price": 65.075000000000003, 
            "type": "currency", 
            "symbol": "AFN=X"
          }
        }
      }, 
      {
.............
}
............

I have tried like this but getting exception :

JSONObject mainObj = null;
try {
    mainObj = new JSONObject(JSON);
    JSONObject getSth = mainObj.getJSONObject("list");
    if(mainObj != null){
        JSONArray list = getSth.getJSONArray("resources");
        if(list != null){
            for(int i = 0; i < list.length();i++){
                JSONObject elem = list.getJSONObject(i);
                if(elem != null){
                    JSONObject prods = elem.getJSONObject("fields");
                    Object level = prods.get("type");
                    Toast.makeText(getApplicationContext(),""+level.toString(),Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}catch (Exception e){
    Toast.makeText(getApplicationContext(),""+e.toString(),Toast.LENGTH_LONG).show();
}

I was getting exception : no values in fields...

And pls give some suggestions that storing these values in Database table(matrotable) of(row fields) name, prize, symbol and type, I may try by making String Array and retrieving and storing the values for sqlite, is there any other easy options... thanks

User Learning
  • 3,165
  • 5
  • 30
  • 51

3 Answers3

2

your fields objects are inside resource object so do

  for(int i = 0; i < list.length();i++){
                JSONObject elem = list.getJSONObject(i);
                if(elem != null){
                    JSONObject prods = elem.getJSONObject("resource")
                                           .getJSONObject("fields");

                    Object level = prods.get("type");
                    Toast.makeText(getApplicationContext(),""+level.toString(),Toast.LENGTH_LONG).show();
                }
            }
"resources": [                      // resources list
  {                                 // object i
    "resource": {                   // fields are inside "resource" object 
      "classname": "Quote", 
      "fields": {
        "date": "2017-03-16", 
        "price": 3.6720000000000002, 
        "type": "currency", 
        "symbol": "AED=X"
      }
    }
  }
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • yes thnx, i need to store to sqlite and then retrieve later, how can i achieve that easily, my go is by adding these fields to string array and retieveing it ? any other suggestions. – User Learning Mar 18 '17 at 18:57
  • 1
    @Learner then you should take a look into `GSON` then you will be able to create a pojo list , try the [GSON docs link](http://stackoverflow.com/documentation/android/4158/gson#t=201703181903413153608) – Pavneet_Singh Mar 18 '17 at 19:00
1

You are missing the resource JOSNObject parsing...

 for(int i = 0; i < list.length();i++){
                JSONObject elem = list.getJSONObject(i);
                JSONObject resource = elem.getJSONObject("resource");
                if(resource != null){
                    JSONObject prods = resource.getJSONObject("fields");
                    Object level = prods.get("type");
                    Toast.makeText(getApplicationContext(),""+level.toString(),Toast.LENGTH_LONG).show();
                }
            }
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • yes thnx, i need to store to sqlite and then retrieve later, how can i achieve that easily, my go is by adding these fields to string array and retieveing it ? any other suggestions. – User Learning Mar 18 '17 at 18:58
  • 1
    take a look at this: [Answer](http://stackoverflow.com/questions/6288500/save-data-from-json-object-to-database-sqlite) create a model class with getters and setters...or another option to use GSON with Pojo.. – rafsanahmad007 Mar 18 '17 at 19:08
0

I recommend to you to use the simplest and easiest way to parse a json response to avoid this kind of issues:

1- generate your model classes by using this tool: http://www.jsonschema2pojo.org/ download and add the generated classes to your model package.

2- add this dependency to your gradle file:

compile 'com.google.code.gson:gson:2.4'

3- Call this method to parse your response:

Gson gson = new Gson();
ResponseModel responseModel = gson.fromJson(json, ResponseModel.class);
Ali Noureddine
  • 324
  • 5
  • 20