{"a":"AAA","b":32.4,"c":34.65},
{"a":"AAM","b":10.8,"c":11.55}
I use JSONArray to get jsonarray above. I want to use this code get data b:10.8
if a:AAM
.
public static void main(String[]args) throws Exception{
String get = sql.getQuote();
JSONArray jsonarray = new JSONArray(get);
for (int i = 0; i<jsonarray.length();i++){
JSONObject obj = jsonarray.getJSONObject(i);
String symbol = obj.getString("a");
if (symbol.equals("AAM")){
double price = obj.getDouble("b");
System.out.println(price);
} else {
break;
}
}
}
Please help me to fix it. Thank you in advance.
The problem is with else
statement in for-loop
.
Remove else
block ie
else {
break;
}
In your code, else
block stops the for-loop
on very first iteration. Because here "a":"AAA"
. Since the condition if (symbol.equals("AAM"))
is false
so else
block is executing which further breaks the loop iteration.
The problem is the break in the else, just take it out. If anywhere, it would be most appropriate in the if part:
if (symbol.equals("AAM")) {
System.out.println(obj.getDouble("b"));
break; // or leave it out if there might be a second AAM entry
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.