遍历数组中的JSONArray [英] Traverse array IN JSONArray
问题描述
我正在做的Android应用程序,这使得调用与Array返回一个JSON作为参数Web服务,我可以通过所有的设置,轻松地保存它们。
I'm making an app for android which makes a call to a webservice that returns a json with ARRAY as a parameter, I can go through all the settings and save them easily.
问题是,当我得到我的JSON对象中返回的数组。
The problem is when I get the array that I returned within the JSON object.
JSON的例子:
[{"codigoArticulo":"0001","nombreArticulo":"CHULETAS DE CORDERO","factorVentasDefecto":"KG","precio":21.95,"factoresDeVenta":["KG","UN"]},{"codigoArticulo":"0007","nombreArticulo":"FALDETA DE CORDERO","factorVentasDefecto":"KG","precio":11.95,"factoresDeVenta":["KG","FL"]}]
我能救codigoArticulo,nombreArticulo,factorVentasDefecto和preCIO容易,但我不知道我可以拯救factoresDeVenta。
I can save "codigoArticulo", "nombreArticulo", "factorVentasDefecto" and "precio easily, BUT i don't know how i can save "factoresDeVenta".
我有这个code:
JSONArray resparray = new JSONArray(JSONdevuelto);
for (int i = 0; i < resparray.length(); i++) {
JSONObject respJSON = resparray.getJSONObject(i);
int IDArticulo = respJSON.getInt("codigoArticulo");
String NombreArticulo = respJSON.getString("nombreArticulo");
String FactordeVenta = respJSON.getString("factorVentasDefecto");
int PrecioArticulo = respJSON.getInt("precio");
}
我怎么可以在一个阵列中节省factoresDeVenta?
How i can save in one array the variables on "factoresDeVenta"?
我尝试
String[] Factores = respJSON.getJSONArray("factoresDeVenta");
但没有工作,因为不兼容的类型。
but no works because are incompatible types.
我需要的数组后做出微调
I need the array to make later a Spinner
感谢您。
推荐答案
factoresDeVenta code>里面的JSONObject一个JSONArray,所以你需要使用
getJSONArray
或 optJSONArray
并使用循环从JSONArray得到的值:
factoresDeVenta
is an JSONArray inside JSONObject so you will need to use getJSONArray
or optJSONArray
and use loop for getting values from JSONArray:
JSONArray jArray = respJSON.optJSONArray("factoresDeVenta");
for (int i = 0; i < jArray.length(); i++) {
String str_value=jArray.optString(i); //<< jget value from jArray
}
这篇关于遍历数组中的JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!