我想从json数组中获取数据,我想为我的条形图制作常规的float数组..但是如何将json数组更改为float数组
public class fragment extends Fragment {
public fragment() {
// Required empty public constructor
}
final static String TAG = "ok";
BarChart barChart;
float datay [];
float datay1 [];
String datax [];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragment = inflater.inflate(R.layout.fragment1, container, false);
barChart = (BarChart)fragment.findViewById(R.id.barchart);
fetchData();
return fragment;
}
public void fetchData(){
showLoading("Loading");
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.CHART, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
hideLoading();
try {
Log.d(TAG,"ini response"+response);
JSONObject object = new JSONObject(response);
//datacatagory for datax
JSONArray datacatagory = object.getJSONArray("category");
Log.d(TAG,"catagory"+datacatagory);
JSONArray jdata1 = object.getJSONArray("data");
Log.d(TAG,"jdata"+jdata1);
JSONObject obj1 = jdata1.getJSONObject(1);
Log.d(TAG,"obj"+obj1);
// arr1 for datay
JSONArray arr1 = obj1.getJSONArray("data");
Log.d(TAG,"arr"+arr1);
//arr2 for datay1
JSONArray arr2 = obj1.getJSONArray("data");
Log.d(TAG,"arr"+arr1);
//how convert data jsonarray to regular array in java??
chartListener(datay,datax,datay1);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
getToast(error.getMessage());
hideLoading();
}
}) {
@Override
protected Map<String, String> getParams() {
return getPOST("laba_rugi");
}
};
addRequestQueen(strReq,TAG);
}
public void chartListener(float datay[],String datax[],float data1[]){
barChart.setDescription("ok");
ArrayList<BarEntry> ydata = new ArrayList<>();
for (int i = 0;i< datay.length;i++){
ydata.add(new BarEntry(datay[i],i));
}
ArrayList<BarEntry> ydata1 = new ArrayList<>();
for (int i = 0;i< datay.length;i++){
ydata1.add(new BarEntry(data1[i],i));
}
ArrayList<String> xdata = new ArrayList<>();
for (int i = 0;i < datax.length;i++){
xdata.add(datax[i]);
}
BarDataSet barDataSet = new BarDataSet(ydata,"rugi");
barDataSet.setColors(ColorTemplate.PASTEL_COLORS);
BarDataSet barDataSet1 = new BarDataSet(ydata1,"laba");
barDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
ArrayList<BarDataSet> dataSets = new ArrayList<>(); // combined all dataset into an arraylist
dataSets.add(barDataSet);
dataSets.add(barDataSet1);
BarData barData = new BarData(xdata,dataSets);
barData.setValueTextSize(13f);
barData.setValueTextColor(Color.BLACK);
int a =00000;
barChart.setData(barData);
barChart.setBackgroundColor(a);
barChart.invalidate();
}
}
我想从json数组中获取数据,我想为我的条形图制作常规的float数组。但是,如何将json数组更改为float数组 请帮帮我。
答案 0 :(得分:2)
这个简单的方法可以帮到你。没有测试,但应该工作。
private float[] fillData(JSONArray jsonArray){
float[] fData = new float[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
try {
fData[i] = Float.parseFloat(jsonArray.getString(i));
} catch (JSONException e) {
e.printStackTrace();
}
}
return fData;
}
只需像这样写这个方法,然后得到浮点数组:
JSONArray arr1 = obj1.getJSONArray("data");
float[] data = fillData(arr1);