简体   繁体   English

使用JSONArray中返回的值(整数)

[英]Use values (integers) returned in JSONArray

Im having issues with being able to get the indivisual responses from this JSON Array. 我在能够从此JSON数组获取个性化响应方面遇到问题。 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 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 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... 例如(prod + prod + prod)等...

Use 采用

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
}

You have the jsonarray as the response 您将jsonarray作为响应

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

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

Do this way 这样做

 JSONArray jsonObj = new JSONArray(jsonStr);

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

do you want to put that json array data in to a new json object? 您想将json数组数据放入新的json对象吗? try this way.. 尝试这种方式..

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

happy cording.. 乐于助人..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM