Java 如何在Android中使用循环数据创建JSON数组
我能够用下面的代码在java中创建JSON数组Java 如何在Android中使用循环数据创建JSON数组,java,android,arrays,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 b
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();
}
int data=20;
JSONArray obj=新的JSONArray();
试一试{
对于(inti=1;i为客户创建一个对象,然后添加到列表中。然后尝试此操作
ArrayList<Customer> list = new ArrayList()<>;
JSONArray jArray = new JSONArray();
for(int i = 0; i < count; count++){
try {
JSONObject cust = new JSONObject();
cust.put("number", list.get(i).number);
cust.put("name", list.get(i).name);
cust.put("url", list.get(i).url);
jArray.put("Cust"+i, cust);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ArrayList list=new ArrayList();
JSONArray jArray=新的JSONArray();
对于(int i=0;i
为客户创建一个对象,然后添加到列表中。然后尝试此操作
ArrayList<Customer> list = new ArrayList()<>;
JSONArray jArray = new JSONArray();
for(int i = 0; i < count; count++){
try {
JSONObject cust = new JSONObject();
cust.put("number", list.get(i).number);
cust.put("name", list.get(i).name);
cust.put("url", list.get(i).url);
jArray.put("Cust"+i, cust);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ArrayList list=new ArrayList();
JSONArray jArray=新的JSONArray();
对于(int i=0;i
JSONArray jsa=new JSONArray();
对于(inti=1;iJSONArray jsa=newjsonarray();
对于(int i=1;我[Solved]感谢Android.K.Doe,我在没有arraylist的情况下修改了您的代码,并添加了int count变量这只是一个示例。它用于动态代码。数据应该来自api。无论如何,没有问题。如果对其他有相同问题的人正确,请接受答案:)[Solved]感谢Android.K.Doe,我在没有arraylist的情况下修改了您的代码,并添加了int count变量。这只是一个示例。它用于动态代码。数据应该来自api。无论如何,没有问题。如果对其他有相同问题的人正确,请接受答案:)