-2

For parsing json i use JSONArray and JsonObject:

 protected void parseJson()
    {
        JSONObject object=null;
        try {

            object=new JSONObject(json);
            myArray=object.getJSONArray(MY_ARRAY);
            Log.e("Array Length",""+myArray.length());
            key_id=new String[myArray.length()];
            key_name=new String[myArray.length()];

            for (int i=0;i<=myArray.length();i++)
            {
                JSONObject fetchObject=myArray.getJSONObject(i);

                    key_id[i] = fetchObject.getString(KEY_ID);
                    key_name[i] = fetchObject.getString(KEY_NAME);


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

JSON DATA:

 {
        "data": [{
            "key": 1,
            "myval": "hello"
        }, {
            "key": 1,
            "myval": "hello"
        }, {
            "key": 1,
            "myval": "hello"
        }, null, {
            "key": 1,
            "myval": "hello"
        }, {
            "key": 1,
            "myval": "hello"
        }]
    }

My problem is that when fetchObject get the null object it give me org.json.JSONObject$1 cannot be converted to JSONObject in try catch . Please help me how can i solve this.....Thanks for reading....

4
  • 2
    Why are you working so hard on parsing code instead of using a json parsing library like GSON is Jackson ? Commented Nov 16, 2016 at 12:39
  • 1
    your json structure is saying something else and your code is doing something else , it's looks suspicious Commented Nov 16, 2016 at 12:45
  • Why the api guy send you a null??? you can ask if key and myval is blank, they can give blank string like { "key": 1, "myval": "" } Then your problem will be solved Commented Nov 16, 2016 at 12:50
  • Hello Manish - I'll come to you asap :) Commented Nov 17, 2016 at 13:55

2 Answers 2

3
if(obj.length() == 0)

is what I would do.

JSONObject fetchObject;

if(flowerArray.getJSONObject(i).length() !=0)

/

fetchObject = flowerArray.getJSONObject(i)
    if(fetchObject== null)
Sign up to request clarification or add additional context in comments.

Comments

0

After reading JsonArray and JsonObject doc i understand how to sort out this problem.

protected void parseJson()
    {
        JSONObject object=null;
        try {

            object=new JSONObject(json);
            myArray=object.getJSONArray(MY_ARRAY);
            Log.e("Array Length",""+myArray.length());
            key_id=new String[myArray.length()];
            key_name=new String[myArray.length()];

            for (int i=0;i<=myArray.length();i++)
            {

                JSONObject fetchObject=myArray.optJSONObject(i);

                    if(fetchObject==null)
                   {
                     //do nothing
                   }
                   else
                   {
                    key_id[i] = fetchObject.getString(KEY_ID);
                    key_name[i] = fetchObject.getString(KEY_NAME);

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

1 Comment

actually it's exactly what nzala told you to try :) :+1: for you nzala

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.