当前位置:   article > 正文

Java构造和解析Json数据-fastjson_java fastjson构造

java fastjson构造

阿里巴巴fastjson介绍

1.maven引用

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.1.39</version>
        <scope>compile</scope>
    </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.代码实类

package com.cjm.mvnbook.test5;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cjm.mvnbook.model.Student;



public class FastJsonTest {

    public static void main(String[] args) {

        String a1 = BeanToJson();
        JsonToBean(a1);

        String a2 = ListToJSON();
        JSONToList(a2);
    }

    //java对象转json
    public static String BeanToJson(){
        Student student = new Student();  
        student.setId(1);  
        student.setName("jag");  
        student.setSex("man");  
        student.setAge(25);  
        student.setHobby(new String[]{"篮球","上网","跑步","游戏"});

        String string = JSONObject.toJSONString(student);
        System.out.println(string);
        return string;
    }
    //json转java对象
    public static void JsonToBean(String s1){

        Student parseObject = JSONObject.parseObject(s1, Student.class);

        System.out.println(parseObject.getName());
    }

    //list转json
    public static String ListToJSON(){
        List<Student> list1 = new ArrayList<Student>();
        Student student = new Student();  
        student.setId(1);  
        student.setName("jag");  
        student.setSex("man");  
        student.setAge(25);  
        student.setHobby(new String[]{"篮球","上网","跑步","游戏"});
        list1.add(student);

        Student student1 = new Student();  
        student1.setId(1);  
        student1.setName("jag");  
        student1.setSex("man");  
        student1.setAge(25);  
        student1.setHobby(new String[]{"篮球","上网","跑步","游戏"});
        list1.add(student1);

        System.out.println(JSONArray.toJSONString(list1));
        System.out.println(JSONObject.toJSONString(list1));

        return JSONArray.toJSONString(list1);
    }

    //json转list
    public static void JSONToList(String s1){

        List<Student> parseObject = JSONArray.parseArray(s1, Student.class);

        System.out.println(parseObject.get(0).getName());
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/941033
推荐阅读
相关标签
  

闽ICP备14008679号