2017-09-14 50 views
0

我正在构建使用http请求消耗Web API的android应用程序。我已经提出要求很容易,一切工作正常,但我找不到一种方法来转换JSONArray我进入单浮点x,浮点y值。 欢迎任何帮助。如何将JSONArray转换为字符串/浮点值

如何API的样子: enter image description here

我的代码:

  @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(); 
      } 


     } 
    }); 

回答

1

只要您可以使用以下方法:

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

另外,price您需要检索应该在的JSONObject代替另一个阵列。

+0

感谢响应! new Entry只接受Float值,但没有getFloat选项,我该如何实现? –

+1

@VaclovasRekašiusJr。你可以改变Entry类型,任何方式检查更新,你都可以获得float的值。 – Ibrahim

+0

感谢您的回复,真的帮了我很多。但更新你的答案,改变这条线 values.add(new Entry((float)jArr.getDouble(0),(float)jArr.getDouble(1))); 删除拳击,只是铸造浮动工作正常。再次感谢。 –

1

你有没有试着用这样的:

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; // 
    } 
} 
+0

不,但我会看看,谢谢你的回答! –