0

I'am fetching the data from the server and storing in my android application. I'am getting the JSONArray name as "project_data" and am having list of JSONObjects in the array. Am iterating the list of JSONObject based on the length of JSONArray.

This is the JSON data where am getting from the server

"project_data": [
    null,
    null,
    {
        "id": "14",
        "name": "JOHN",
        "short_name": "JH",          
    }
]

This is the error what am facing

value null at 0 of type org.json.jsonobject$1 cannot be converted to jsonobject

This is my code

JSONArray projectData=job.getJSONArray("project_data");
for(int i=0;i<projectData.length();i++)
        {               
          JSONObject proj=projectData.getJSONObject(i);
          Log.i("projectdetails",proj.toString());
          pro = Project.getProject(getValue(proj,"id"));
          if(pro==null)
              pro=new Project();
          pro.p_id = getValue(proj,"id");
          pro.name = getValue(proj,"name");
          pro.short_name = getValue(proj,"short_name");
          pro.save();
        }

I'am facing the error in this line. JSONObject proj=projectData.getJSONObject(i);

Please help me to solve this problem.

user3764346
  • 129
  • 2
  • 5
  • 15

3 Answers3

2

This worked for me.

 Object obj = jArray.get(i);
 if (obj != null && !obj.toString().equals("null")) {
    JSONObject jObj = jArray.getJSONObject(i);
    //Make parse here
 }
Dansp
  • 1,378
  • 2
  • 14
  • 36
0

Can you change data from server ? You must get from PHP server data like this

{"project_data": [{"id": "14","name": "JOHN","short_name": "JH" }]}

Then decoding JSON object is below.

public class MainActivity extends Activity {

TextView txtView;
TextView txtView1;
int i=0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    txtView = (TextView) findViewById(R.id.txtView);
    txtView1 = (TextView) findViewById(R.id.txtView1);

    //String job = "'project_data': [null,null,{'id': '14','name': 'JOHN','short_name': 'JH', }]"; //bad data
    String job = "{'project_data': [{'id': '14','name': 'JOHN','short_name': 'JH' }]}"; 




    txtView.setText(job);

    try{


        String s =" " ;
        JSONObject jsnobject = new JSONObject(job);

        JSONArray jsonArray = jsnobject.getJSONArray("project_data");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject explrObject = jsonArray.getJSONObject(i);

            s= s+   "name: " + explrObject.getString("name")+"\n" +
              "short_name: "+ explrObject.getString("short_name") +"\n";


        }


        txtView1.setText(s);
    }catch(Exception e){

        txtView.setText( "error 3 "+i+" "+ e.toString());
    }



}


}
eurosecom
  • 2,932
  • 4
  • 24
  • 38
0

Well server should not give you null data but for particular if you really want to get the result you may try removing for loop then

JSONObject proj=projectData.getJSONObject(2);//index of your json object in projectData array, 0,1 are null so you are getting that error
Sushant
  • 1,834
  • 19
  • 32
  • 1
    Yes I agree with you. Server should not return null but if it happens in some situation we have to handle from the android part. My one more doubt is if i get this null values in shuffle manner then how can i handle this? Right now I'am not facing this situation. But in future i should handle this problem. Could you give me an idea? – user3764346 Nov 11 '14 at 03:13
  • you can always check if (jsonObject != null) – Sushant Nov 11 '14 at 07:36