Vaclovas Rekašius Jr.
Vaclovas Rekašius Jr.

Reputation: 401

how to convert JSONArray to string/float values

I am building an android app that uses HTTP request for consuming web API. I've made request pretty easily, everything works fine, but I can't find a way to convert the JSONArray that I am getting into single Float x, Float y values. Any help would be welcome.

How the API looks :

enter image description here

My code:

@Override
public void onResponse(Call call, Response response) throws IOException {
            try {
                JSONObject jsonObj = new JSONObject(response.body().string());

                // Getting JSON Array node
                JSONArray price = jsonObj.getJSONArray("price");

                for(int I = 0;  I < price.length(); I++) {
                    Float x ;
                    Float y ;

                    values.add(new Entry(x,y)); // add one entry per hour
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
});

Upvotes: 0

Views: 2793

Answers (2)

user6490462
user6490462

Reputation:

Simply you can use following way:

JsonArray jArr = price.getJSONArray(I);
values.add(new Entry((float)jArr.getDouble(0), (float)jArr.getDouble(1)));

Also the price you need to retrieve should be in jsonObject instead of another array.

Upvotes: 1

Osvaldo Bringaz
Osvaldo Bringaz

Reputation: 127

Did you try with like:

private void getPricesArray(){
    ArrayList<Double[]> pricesDoubleArray = new ArrayList<>();
    for (int index = 0; index < pricesDoubleArray.size(); index++) {
        pricesDoubleArray.get(index)[0] = Double.valueOf(16748584949L); //
        pricesDoubleArray.get(index)[1] = 4506.45; //
    }
}

Upvotes: 1

Related Questions