json - Cannot find symbol variable Android Studio -
i working on parsing data json url. jsonobjects have different keys. want data each json object , when doesn't have key want give default message.
this i'm trying use:
if(myjsonobject.has("mykey")) { <- in case "abv" //it has it, appropriate processing }
i got variable
private static final string tag_abv = "abv";
i tried doing check if abv key included in json , give string default text of "no value" when not inculed.
if (jsonstr != null) { try { jsonobject jsonobj = new jsonobject(jsonstr); // getting json array node data = jsonobj.getjsonarray(tag_data); // looping through (int = 0; < data.length(); i++) { jsonobject c = data.getjsonobject(i); if(c.has("abv")) { string abv = c.getstring(tag_abv); } else { string abv = "no value"; } hashmap<string, string> data = new hashmap<string, string>(); // adding each child node hashmap key => value data.put(tag_abv, abv); datalist.add(data); } } catch (jsonexception e) { e.printstacktrace(); } } else { log.e("servicehandler", "couldn't data url"); } return null; }
but have error: cannot find symbol variable abv guess abv inside if statement out scope.
you're declaring abv
inside if/else blocks, means it's accessible inside block. when try use later (in data.put(tag_abv, abv);
variable created no longer accessible - it's out of scope. if move declaration of abv
line before if/else statement, should fix error.
Comments
Post a Comment