java - 如何在 Android 中使用循环数据创建 JSON 数组

标签 java android arrays json

我能够使用下面的代码在 java 中创建 JSON 数组

JSONObject cust1 = new JSONObject();
    try {
        cust1.put("number", "1");
        cust1.put("name", "customer1");
        cust1.put("url", "url1");

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    JSONObject cust2 = new JSONObject();
    try {
        cust2.put("number", "2");
        cust2.put("name", "customer3");
        cust2.put("url", "url3");

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    JSONObject cust3 = new JSONObject();
    try {
        cust3.put("number", "3");
        cust3.put("name", "customer3");
        cust3.put("url", "url3");

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    JSONObject cust4 = new JSONObject();
    try {
        cust4.put("number", "4");
        cust4.put("name", "customer4");
        cust4.put("url", "url4");

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JSONObject cust5 = new JSONObject();
    try {
        cust5.put("number", "5");
        cust5.put("name", "customer5");
        cust5.put("url", "url5");

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JSONArray jsonArray = new JSONArray();

    jsonArray.put(cust1);
    jsonArray.put(cust2);
    jsonArray.put(cust3);
    jsonArray.put(cust4);
    jsonArray.put(cust5);

    String jsonStr = jsonArray.toString();

    System.out.println(jsonStr);

输出 JSON

[{"number":"1","name":"customer1","url":"url1"},{"number":"2","name":"customer2","url":"url2"},{"number":"3","name":"customer3","url":"url3"},{"number":"4","name":"customer4","url":"url4"},{"number":"5","name":"customer5","url":"url5"}]

因为我有 20 个客户,如何在有效的代码中创建循环?我的意思是,只需更改最后一个字符的一个数字即可。

感谢 Android.K.Doe 和 Randyka Yudhistira 解决

int data = 20;
    JSONArray obj = new JSONArray();
    try {
        for(int i=1;i<=data;i++){
            // 1st object

            JSONObject cust= new JSONObject();
            cust.put("number",String.valueOf(i));
            cust.put("name","customer"+i);
            cust.put("url","url"+i);

            obj.put(cust);
        }
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

最佳答案

JSONArray jsa = new JSONArray();
for (int i = 1; i<= 20; i++) {
  JSONObject cust = new JSONObject();
  cust.put("number",String.valueOf(i));
  cust.put("name","customer"+i);
  cust.put("url","url"+i);
  jsa.put(cust);
}
System.out.println(jsa.toString());

关于java - 如何在 Android 中使用循环数据创建 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39381147/

相关文章:

android - 在Android上流式传输m3u8 HLS音频

javascript - 如果索引大于 arr 长度,它应该再次返回

java - 如何在 ListView 中显示使用文件写入器保存的项目

java - Android SQlite 更新只是不工作,没有错误

android - Flutter:相当于NSLocale (iOS) & Locale (Android)

javascript - 将 JSON 对象转换为 JS 数组

javascript - 如何找到与两个数组相交的所有对象?

java - 在 getTables() 方法中的何处指定数据库名称。

java - 如何设置 LogBack 配置

java - 动画菜单实现