0

My app fetches JSON data from a server and stores them in a CardView. All the data is processed correctly, but if some data isn't available at the moment the JSONArray is empty. The app fetches them anyway and writes 'null' into the TextView, while I want to have it empty. I already searched other related questions and thought it was the best way to check the length of the array, but still it puts 'null' in my TextView:

What I get:

(null - null)

What I want to have:

( - )

This is the structure of what is fetched:

[
 {
  "MatchResults": []
 }
]

How I tried to handle it:

JSONArray jArray = json.getJSONArray("MatchResults");

for(int j=0; j<jArray.length(); j++) {

    JSONObject json_data = jArray.getJSONObject(j);

         if(jArray != null && jArray.length() > 0){
             GetDataAdapter.setTore1(json_data.getString(TEAM1));
             GetDataAdapter.setTore2(json_data.getString(TEAM2));
         }
}

What do I have to change so that the setTore1 and setTore2 method leave the TextView empty?

5
  • This example of json won't enter the loop. Commented Sep 2, 2018 at 9:38
  • Why not? I cut the code of course, I hope I didn't cut anything important Commented Sep 2, 2018 at 9:40
  • Because the MatchResults array is empty. Or is it not the data you are working with? In that case provide an actual example. Commented Sep 2, 2018 at 9:41
  • Oh yes, of course! I didn't think about that correctly, with that hint I was able to solve it. Thank you very much! Commented Sep 2, 2018 at 9:48
  • I provided a more detailed answer if it can be useful. Commented Sep 2, 2018 at 9:50

1 Answer 1

1

The example you provided probably doesn't match the provided output, as mentioned in the comment. Anyway, what you are describing can be done by simply initializing the properties in construction phase.

I am not sure of the object that contains the properties, but those should be tore1 and tore2. Before iterating the json and try to set those, just initialize them to be empty strings ("") in this way when you don't find a valid value in the json, you will use the empty string as required.

Sign up to request clarification or add additional context in comments.

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.