当前位置:   article > 正文

Java(1):java中list集合存JSONObject对象,遍历list取json值_list

list

本次需求:

需求1:需要生成1000-5000个json值(其中1个值是随机数),并放入list中

其中1个json值如下:

{"keyId":"1404623066687889410“,"metadata":"ZIV5JyLgOK"}

ZIV5JyLgOK是随机数生成

需求2:需要把list转换成字符串

需求3:需要遍历生成json list取到对应的值,其他算法调用。

解决方法:

1、随机数生成的方法

方法说明:从a-z,以及0-1中生成随机数,输入长度就可以得到长度几的随机数

  1. import java.util.Random;
  2. public static String getRandomString(int length) {
  3. String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  4. Random random = new Random();
  5. StringBuffer sb = new StringBuffer();
  6. for (int i = 0; i < length; i++) {
  7. int number = random.nextInt(62);
  8. sb.append(str.charAt(number));
  9. }
  10. return sb.toString();
  11. }

2、生成1000个json值,并放入jsonlist中

//生成随机数,metadata赋值随机数

String metadata=getRandomString(10);

//object_json putjson值

//jsonList添加json

  1. import org.json.JSONObject; //--->需要引入json-20210307.jar
  2. import java.util.Random;
  3. import java.util.List;
  4. import java.util.ArrayList;
  5. JSONObject object_json = new JSONObject();
  6. List<JSONObject> jsonList = new ArrayList<JSONObject>();
  7. for (int i=0;i<1000;i++){
  8. String metadata=getRandomString(10);
  9. //System.out.println(metadata);
  10. object_json.put("keyId", "1404623066687889410");
  11. object_json.put("metadata", metadata);
  12. //list添加json
  13. //objar_list.add(object_json);
  14. jsonList.add(object_json);
  15. }
  16. System.out.println(jsonList); //打印list
  17. System.out.println(jsonList.size()); //打印list长度

3、把list转换成字符串,其他地方需要应用

        String aaa=jsonList.toString();

4、获取字段的数据类型

  1. System.out.println(getType(aaa));
  2. private static String getType(Object a) {
  3. return a.getClass().toString();
  4. }

5、遍历Jsonlist

  1. for(JSONObject json : jsonList) {
  2. String metadata = json.getString("metadata");
  3. String keyId = json.getString("keyId");
  4. }

全部代码:

  1. //import java.util.*;
  2. import org.json.JSONObject;
  3. //import org.json.JSONArray;
  4. //import org.json.JSONException;
  5. import java.util.Random;
  6. import java.util.List;
  7. import java.util.ArrayList;
  8. public class Test2 {
  9. public static void main(String[] args) throws Exception {
  10. //String aaa=getRandomString(10);
  11. //System.out.println(aaa);
  12. JSONObject object_json = new JSONObject();
  13. //List objar_list=new ArrayList();
  14. List<JSONObject> jsonList = new ArrayList<JSONObject>();
  15. for (int i=0;i<1000;i++){
  16. String metadata=getRandomString(10);
  17. //System.out.println(metadata);
  18. object_json.put("keyId", "1404623066687889410");
  19. object_json.put("metadata", metadata);
  20. //list添加json
  21. //objar_list.add(object_json);
  22. jsonList.add(object_json);
  23. }
  24. //System.out.println(objar_list);
  25. System.out.println(jsonList);
  26. System.out.println(jsonList.size());
  27. String aaa=jsonList.toString();
  28. System.out.println(getType(aaa));
  29. System.out.println("String: " + aaa);
  30. for(JSONObject json : jsonList) {
  31. String metadata = json.getString("metadata");
  32. String keyId = json.getString("keyId");
  33. }
  34. }
  35. public static String getRandomString(int length) {
  36. String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  37. Random random = new Random();
  38. StringBuffer sb = new StringBuffer();
  39. for (int i = 0; i < length; i++) {
  40. int number = random.nextInt(62);
  41. sb.append(str.charAt(number));
  42. }
  43. return sb.toString();
  44. }
  45. private static String getType(Object a) {
  46. return a.getClass().toString();
  47. }
  48. }

执行结果:

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/618583
推荐阅读
相关标签
  

闽ICP备14008679号