Reading exception from JSON
I have this code
JSONObject obj; try { obj = new JSONObject(readUrl("http://dleel.ps/ss.txt")); List<String> list = new ArrayList<String>(); JSONArray array = obj.getJSONArray("data"); for(int i = 0 ; i < array.length() ; i++) { if (array.getJSONObject(i).getString("link")!=null) System.out.println(array.getJSONObject(i).getString("link")); } } Why do I get an exception when there is no link (JSONObject ["link"] not found.), What should I put in the if condition? I also tried using optJSONArray instead of getJSONArray, but the same
The getString () method throws an exception if the key is not found. Use the has () method instead:
if (array.getJSONObject(i).has("link"))