【问题标题】:Passing json to server containing json array将 json 传递给包含 json 数组的服务器
【发布时间】:2019-09-14 20:35:03
【问题描述】:

我正在尝试在 JSON OBJECT 中传递一个 json 数组。问题是我的数组列表只包含 id,我必须循环并传递给 jsonarray,但我的 jsonarray 只获取 arraylist 的最后一个索引的值并复制到 arraylist 大小的时间。

            JSONObject jsonObject = new JSONObject();
            JSONArray category_ids_array = new JSONArray();
            JSONObject category_items = new JSONObject();


            jsonObject.put("category_ids", category_ids_array);


            for (int g=0 ;g < selectedCategories.size();g++)
            {
                category_ids_array.put(category_items);

                category_items.put("id",selectedCategories.get(g).getId());
                Log.d("catids",String.valueOf(selectedCategories.get(g).getId() ) );

            }

selectedCategories 数组列表包含: 1937,1994,13365;

jsononject: {"category_ids":[{"id":13365},{"id":13365},{"id":13365}]}

【问题讨论】:

  • 请添加你的json
  • 更新了我的答案
  • 请格式化您的问题
  • 先生,我需要格式化什么?

标签: android json arraylist


【解决方案1】:

实际上我知道我做错了什么。我每次都需要新建一个 JSONObject。

for (int g=0 ;g < selectedCategories.size();g++)
        {
            JSONObject category_items = new JSONObject();

            category_items.put("id",selectedCategories.get(g).getId());
            Log.d("catids",String.valueOf(selectedCategories.get(g).getId() ) );

category_ids_array.put(category_items);

        }

【讨论】:

    猜你喜欢
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 2011-11-13
    相关资源
    最近更新 更多