• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps retrieve values from Json

AlexeyS

Newbie
Hello. I retrieve float variable from Json Object and it's works wonderful.

float grade = Float.parseFloat(root.getString("grade_json));

The problem is that sometimes, there are no grade, and the value inside grade_json is "N/A" and i get an error.

How can i solve this problem? I want to put Null into grade and continue the app.

Thank you
 
Last edited:
First, you can't put null into float, You must use Float for null.

Java:
Float grade = null;

try {
  grade = Float.parseFloat(root.getString("grade_json));
}
catch (Exception ign) {
   grade = null;  // Here you can put what you need for N/A value
}
 
Last edited:
Back
Top Bottom