AI智能
改变未来

FastJson入门学习(1)


FastJson学习(1)

初学者,多多指正!

需要导入的json-jar包
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.62</version></dependency>
@JSONField(name = “id1”)此注解中属性详解:
name就是key,而id的值就是value值
ordinal表示对字段进行排序
format属性可以对一些需要规范化的数据进行格式化,例如日期
public class Person {@JSONField(name = \"id1\",ordinal = 1)private int id;@JSONField(name = \"yourName1\",ordinal = 2)private String yourName;@JSONField(name = \"birthday1\",format = \"yyyy-MM-dd\",ordinal = 3)private Date birthday;public Person() {}public Person(int id, String yourName, Date birthday) {this.id = id;this.yourName = yourName;this.birthday = birthday;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getYourName() {return yourName;}public void setYourName(String yourName) {this.yourName = yourName;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}@Overridepublic String toString() {return \"Person{\" +\"id=\" + id +\", yourName=\'\" + yourName + \'\\\'\' +\", birthday=\" + birthday +\'}\';}}
测试
public class JsonTest {public static void main(String[] args) {List<Person> personList = new ArrayList<>();personList.add(new Person(15,\"wy\",new Date()));personList.add(new Person(17,\"hp\",new Date()));String jsonOutput = JSON.toJSONString(personList);System.out.println(jsonOutput);}}
结果展示
[{\"id1\":15,\"yourName1\":\"wy\",\"birthday1\":\"2020-08-05\"},{\"id1\":17,\"yourName1\":\"hp\",\"birthday1\":\"2020-08-05\"}]
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » FastJson入门学习(1)