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

3 Answers 3

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"
      }
    }
  }
Sign up to request clarification or add additional context in comments.

2 Comments

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.
@Learner then you should take a look into GSON then you will be able to create a pojo list , try the GSON docs link
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();
                }
            }

2 Comments

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.
take a look at this: Answer create a model class with getters and setters...or another option to use GSON with Pojo..
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.