1

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.

The problem is when I get the array that I returned within the JSON object.

Example of 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"]}]

I can save "codigoArticulo", "nombreArticulo", "factorVentasDefecto" and "precio easily, BUT i don't know how i can save "factoresDeVenta".

i have this 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");
}

How i can save in one array the variables on "factoresDeVenta"?

I try

String[] Factores = respJSON.getJSONArray("factoresDeVenta");

but no works because are incompatible types.

I need the array to make later a Spinner

Thank you.

3
  • 2
    Calling respJSON.getJSONArray("factoresDeVenta") doesn't work? Commented Jun 6, 2013 at 10:41
  • But i need the values inside and i write "String[] Factores = respJSON.getJSONArray("factoresDeVenta");" and are incompatible types Commented Jun 6, 2013 at 10:44
  • JSONArray Factores = respJSON.getJSONArray("factoresDeVenta") and than iterate the JSONArray to transform it String[] Commented Jun 6, 2013 at 10:46

3 Answers 3

3

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
 }
Sign up to request clarification or add additional context in comments.

Comments

1

You can store in ArrayList<String>

ArrayList<String> arrStr = new ArrayList<String>();

JSONArray jArray = respJSON.optJSONArray("factoresDeVenta");

for (int i = 0; i < jArray.length(); i++) {
     arrStr.add(jArray.getString(i));    
}

And then you can convert arraylist to string array

String[] Factores = arrStr.toArray(new String[arrStr.size()]);

Comments

0
JSONArray jArray = respJSON.getJSONArray("factoresDeVenta");

It would give you the array of data stored in factoresDeVenta, which you can again traverse to get the individual elements from that array.

2 Comments

within elements are varying, so I want to pick automatic mode and not the "getString ()", I can?
jArray.get(n) would give you the nth element of jArray.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.