赞
踩
本次需求:
需求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 = random.nextInt(62);
- sb.append(str.charAt(number));
- }
- return sb.toString();
- }
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 aaa=jsonList.toString();
4、获取字段的数据类型
- System.out.println(getType(aaa));
-
- private static String getType(Object a) {
- return a.getClass().toString();
- }
5、遍历Jsonlist
- for(JSONObject json : jsonList) {
- String metadata = json.getString("metadata");
- String keyId = json.getString("keyId");
- }
全部代码:
- //import java.util.*;
- import org.json.JSONObject;
- //import org.json.JSONArray;
- //import org.json.JSONException;
- import java.util.Random;
- import java.util.List;
- 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 aaa=jsonList.toString();
- System.out.println(getType(aaa));
- System.out.println("String: " + aaa);
-
- for(JSONObject json : jsonList) {
- String metadata = json.getString("metadata");
- String keyId = json.getString("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 = random.nextInt(62);
- sb.append(str.charAt(number));
- }
- return sb.toString();
- }
-
- private static String getType(Object a) {
- return a.getClass().toString();
- }
- }
执行结果:
C:\Java\jdk1.8.0_211\jre\lib\rt.jar;C:\java_project2021\json-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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。