Java(1):java中list集合存JSONObject对象,遍历list取
json值
本次需求:
需求1:需要⽣成1000-5000个json值(其中1个值是随机数),并放⼊list中
其中1个json值如下:
{"keyId":"1404623066687889410“,"metadata":"ZIV5JyLgOK"}
ZIV5JyLgOK是随机数⽣成
需求2:需要把list转换成字符串
需求3:需要遍历⽣成json list取到对应的值,其他算法调⽤。
解决⽅法:
1、随机数⽣成的⽅法
⽅法说明:从a-z,以及0-1中⽣成随机数,输⼊长度就可以得到长度⼏的随机数
import java.util.Random;
public static String getRandomString(int length) {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = Int(62);
sb.append(str.charAt(number));
}
String();
}
2、⽣成1000个json值,并放⼊jsonlist中
//⽣成随机数,metadata赋值随机数
String metadata=getRandomString(10);
//object_json putjson值
//jsonList添加json
import org.json.JSONObject; //--->需要引⼊json-20210307.jar import java.util.Random;
import java.util.List;
import java.util.ArrayList;
JSONObject object_json = new JSONObject();
List<JSONObject> jsonList = new ArrayList<JSONObject>(); for (int i=0;i<1000;i++){
String metadata=getRandomString(10);
//System.out.println(metadata);
object_json.put("keyId", "1404623066687889410");
object_json.put("metadata", metadata);
//list添加json
//objar_list.add(object_json);
jsonList.add(object_json);
}
System.out.println(jsonList); //打印list
System.out.println(jsonList.size()); //打印list长度
3、把list转换成字符串,其他地⽅需要应⽤
String String();
4、获取字段的数据类型
System.out.println(getType(aaa));
private static String getType(Object a) {
Class().toString();
}
5、遍历Jsonlist
for(JSONObject json : jsonList) {
String metadata = String("metadata");
String keyId = String("keyId");
}
全部代码:
//import java.util.*;
import org.json.JSONObject;
//import org.json.JSONArray;
//import org.json.JSONException;
import java.util.Random;
import java.util.List;nextint()方法
import java.util.ArrayList;
public class Test2 {
public static void main(String[] args) throws Exception {
//String aaa=getRandomString(10);
/
/System.out.println(aaa);
JSONObject object_json = new JSONObject();
//List objar_list=new ArrayList();
List<JSONObject> jsonList = new ArrayList<JSONObject>();
for (int i=0;i<1000;i++){
String metadata=getRandomString(10);
//System.out.println(metadata);
object_json.put("keyId", "1404623066687889410");
object_json.put("metadata", metadata);
//list添加json
//objar_list.add(object_json);
jsonList.add(object_json);
}
//System.out.println(objar_list);
System.out.println(jsonList);
System.out.println(jsonList.size());
String String();
System.out.println(getType(aaa));
System.out.println("String: " + aaa);
for(JSONObject json : jsonList) {
String metadata = String("metadata");
String keyId = String("keyId");
}
}
public static String getRandomString(int length) {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = Int(62);
sb.append(str.charAt(number));
}
String();
}
private static String getType(Object a) {
Class().toString();
}
}
执⾏结果:
C:\Java\jdk1.8.0_211\ -
javaagent:C:\Java\IntelliJ_IDEA_2018.2.4\lib\idea_rt.jar=58798:C:\Java\IntelliJ_IDEA_2018.2.4\bin -ding=UTF-8 -classpath
C:\Java\jdk1.8.0_211\jre\lib\charsets.jar;C:\Java\jdk1.8.0_211\jre\lib\deploy.jar;C:\Java\jdk1.8.0_211\jre\lib\ext\access-
bridge-
64.jar;C:\Java\jdk1.8.0_211\jre\lib\ext\cldrdata.jar;C:\Java\jdk1.8.0_211\jre\lib\ext\dnsns.jar;C:\Java\jdk1.8.0_211\jre\lib\ext\jac agent.jar;C:\Java\jdk1.8.0_211\jre\lib\plugin.jar;C:\Java\jdk1.8.0_211\jre\lib\resources.jar;C:\Java\jdk1.8.0_211\jre\lib\rt.jar;C:\ja
20210307.jar;C:\java_project2021\javatest\out\production\javatest Test2
1000
class java.lang.String
String: [{"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":
"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}, {"metadata":"hiQCVqSFPM","keyId":"1404623066687889410"}]
Process finished with exit code 0
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论