Reputation: 499
in my json data i have data in Array as string e.g:
//
"values": "{{0, 25}, {96.86, 184}}",
"matrix": "[1, 7, 0, 9, 0, 0]",
//
now, I getting the data as String. how can i convert the data to float[]?
Upvotes: 2
Views: 2026
Reputation: 11642
You can parse like this,
try {
JSONObject object = new JSONObject("[your Json String]");
String value = object.optString("values");
String floatStr = value.replace("{", "").replace("}", "");
String[] valuesArr = floatStr.split(",");
float[] floatArr = new float[valuesArr.length];
for (int i = 0; i < valuesArr.length; i++) {
String floatString = valuesArr[i];
if (TextUtils.isEmpty(floatStr) || TextUtils.isEmpty(floatStr.trim())) {
floatArr[i] = 0.0f;
continue;
}
floatArr[i] = Float.parseFloat(floatString.trim());
}
for (int i = 0; i < floatArr.length; i++) {
Log.d(TAG, "value : at " + i + " is " + floatArr[i]);
}
} catch (JSONException e) {
e.printStackTrace();
}
Upvotes: 2
Reputation: 1
İf I dont understand wrong Do it first :Sprit your character with String tokenizer then every String seems like number Last thing you have to do take to part of String and put in float Array
Upvotes: 0
Reputation: 4869
Loop through your json data and use
Float.parseFloat(your_float_value_in_string)
Upvotes: 0