暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

json使用

元素周期表A 2021-06-28
457
    public static void main(String[] args) {

    List<Student> list=new ArrayList<>();

    Student student=new Student("bob",24);
    Student student12=new Student("lily", 23);

    list.add(student);
    list.add(student12);
    System.out.println("*******JSON.toJSONString(student)*******");
    String str1=JSON.toJSONString(student);
    System.out.println(str1);
    System.out.println(JSON.toJSONString(list));
    System.out.println();


    System.out.println("******JSON.parseObject(str1,Student.class)*******");
    //Student stu1=JSON.parseObject(str1,new TypeReference<Student>(){});
    Student stu1=JSON.parseObject(str1,Student.class);
    System.out.println(stu1);
    System.out.println();

    System.out.println("******j(JSONObject)JSON.toJSON(student)t******");
    JSONObject jsonObject1=(JSONObject)JSON.toJSON(student);
    System.out.println(jsonObject1.getString("name"));
    System.out.println();

    System.out.println("******JSON.toJavaObject(jsonObject1, Student.class)*****");
    Student student2=JSON.toJavaObject(jsonObject1, Student.class);
    System.out.println(student2);
    System.out.println();

    System.out.println("*******(JSONArray)JSON.toJSON(stulist)******");
    List<Student> stulist=new ArrayList<>();
    for(int i=0;i<5;i++){
    stulist.add(new Student("student"+i, i));

    }
    JSONArray jsonArrays=(JSONArray)JSON.toJSON(stulist);
    for(int i=0;i<jsonArrays.size();i++){
    System.out.println(jsonArrays.getJSONObject(i));
    }
    System.out.println();

    System.out.println("*****jjsonArrays.getJSONObject(i), Student.class*****");
    List<Student> myList=new ArrayList<>();
    for(int i=0;i<jsonArrays.size();i++){

    Student student3=JSON.toJavaObject(jsonArrays.getJSONObject(i), Student.class);
    myList.add(student3);
    }
    for(Student stu:myList){
    System.out.println(stu);
    }

    System.out.println();

    System.out.println("*****JSON.toJSONString(jsonObject1);*****");
    String str4=JSON.toJSONString(jsonObject1);
    System.out.println(str4);
    System.out.println();

    System.out.println("*******JSON.parseObject(str1);*****");
    JSONObject jso1=JSON.parseObject(str1);
    System.out.println(jso1.getString("name"));
    System.out.println();

    System.out.println("*****JSON.parseArray(JSON.toJSONString(stulist)*****");
    JSONArray jArray=JSON.parseArray(JSON.toJSONString(stulist));
    for(int i=0;i<jArray.size();i++){
    System.out.println(jArray.getJSONObject(i));
    }
    System.out.println();
    }
    复制


    文章转载自元素周期表A,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

    评论