0

Im having issues with being able to get the indivisual responses from this JSON Array. Im trying to write some code that will allow me to use the vaules indivisually, as well as how to combine them if i needed too. These responses are always integers.

Java

JSONObject jsonObj = new JSONObject(jsonStr);
data_array = jsonObj.getJSONArray("data");

  for (int i = 0; i < data_array.length(); i++) {

      JSONObject prod = data_array.getJSONObject(i);
          Log.d("Logging Response", prod);
}

Json

[5652,8388,8388,7537,8843,2039,8235,0,12220]

I'd like to figure out how i can add them together and return the calculated result, as well as how to use them individually. such as (prod + prod + prod) etc...

3
  • 1
    you are getting [5652,8388,8388,7537,8843,2039,8235,0,12220] String in Response or it is inside inner JSONObject of JSONArray? Commented Feb 6, 2014 at 7:22
  • I'm not quite sure if I get you right, your question is about to iterate over an array or product? In this case you can use a "for (var n in yourObject)". Commented Feb 6, 2014 at 7:23
  • do you want to put that json array data in to a new json object? Commented Feb 6, 2014 at 7:25

4 Answers 4

1
JSONArray data = jsonObj.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
    Integer prod = (Integer) data.get(i);
    System.out.println("Prod " + prod);
    // loop and add it to array or arraylist
}
Sign up to request clarification or add additional context in comments.

Comments

1

You have the jsonarray as the response

ArrayList<Integer> jsonvalue=new ArrayList<Integer>();
    for (int i = 0; i < data_array.length(); i++) {

      int i=     data_array.getInt(i);
jsonValue.add(i)
        }

Comments

1

To put that json array data in to a new json object, try this:

JSONObject jsonObj = new JSONObject(jsonStr);
data_array = jsonObj.getJSONArray("data");
JSONObject prod;

for (int i = 0; i < data_array.length(); i++) {
  prod.put(i,data_array.getJSONObject(i));
      Log.d("Logging Response", prod);
}

Comments

1
 JSONArray jsonObj = new JSONArray(jsonStr);
            
            for (int i = 0; i < jsonObj.length(); i++) {
                System.out.println(jsonObj.getString(i));
            }

Comments

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.