0

I asked a similar question not too long ago, but having further issues.

I have a VALID JSON Array:

However it does not read task_goalid as it is in the second part of the JSON array, error coming up as 'No Value for task_goalid'.

Any idea how to refrence the 2nd part of the JSON array?

3
  • When does the curlybrace end? for(int y=0;y<jArray.length();y++){ // <-I mean this. Commented Apr 1, 2013 at 21:49
  • It ends after the logs. Commented Apr 1, 2013 at 22:10
  • Then look at MCeleys answer, which explains your situation... Commented Apr 1, 2013 at 22:12

2 Answers 2

3

use JSONObject.has() for checking if an JSONObject contain an key or not before getting value of key from JSONObject.

for example in your case:

 String strgoalid;
 if(json_data.has("task_goalid")){

     //if json_data JSONObject contain task_goalid
   strgoalid=json_data.getString("task_goalid")
  }
  else{
    //if json_data JSONObject not contain task_goalid
    strgoalid="default value here";
  }
Sign up to request clarification or add additional context in comments.

Comments

1

You have a JSONArray containing two JSONObject children.

The first JSONObject has the following fields:

  • _id
  • task_name
  • task_day
  • task_priority
  • task_description
  • task_goal
  • user

The second JSONObject has the following fields:

  • _id
  • task_goalid
  • user

Since your first JSONObject doesn't not contain the task_goalid field, you won't be able to retrieve it from that object. If the field is optional, which it appears to be, you can either check if the field exists first before trying to get it using JSONObject.has("task_goalid") or get it as an optional string using JSONObject.optString("task_goalid", "No Value").

Using JSONObject.has() is the safer option here since you can definitively say whether that field exists or not. Using JSONObject.optString() allows you to assign a default value to missing fields more easily. Either method will work so it's up to you which one better suits your needs.

5 Comments

I will be using multiple JSONObjects, how do I specifically get the fields from the second JSONObject?
The second object is at index 1. To get the first object you'd use jArray.getJSONObject(0). To get the second, you'd use jArray.getJSONObject(1). Note that this isn't the best way of doing things as your array might not always be setup the same way each time. It'd be best to iterate through your array and check which object has the fields you're looking for first and only get the objects that have the required fields.
Thanks for the detailed and very clear help you have provided!Using (1) got me the goal id!
Just a quick note, I looped through all objects in the first place (0....1), you would of though it would have picked up the 'goalid' but it didnt? I have to manually refrence JSONObject(1) to get the goalid?
If you're looping through all of them and you call json_data.getString("task_goalid") and there is no task_goalid field in the object, it'll throw an exception. This will break you out of your for loop and drop down to your catch(JSONException e) block which I assume is further down in your code.

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.