码迷,mamicode.com
首页 > Web开发 > 详细

一个简单的JSONDEMO

时间:2020-01-06 11:15:37      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:ons   password   user   parse   set   out   return   ali   javabean   

 用到的bean

 

public class User {
private int id;
private String name;
private String password;

public User() {
}

public User(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", name=‘" + name + ‘\‘‘ +
", password=‘" + password + ‘\‘‘ +
‘}‘;
}
}

华丽的分割线:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

json转换的Bean

package com.example.demo.text;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.bean.User;

/**
* @date 2020/1/6 10:11
*/
public class t1 {
public static void main(String[] args) {
User user = new User(1,"老王","老王");
// 转成Json
String json = JSON.toJSONString(user);
System.out.println(json);
final Object parse = JSON.parse(json);
System.out.println(parse);
// 变成JavaBean对象
final User user1 = JSONObject.parseObject(json, User.class);
System.out.println(user1);
}
}
华丽的分割线:——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
输出结果

{"id":1,"name":"老王","password":"老王"}
{"password":"老王","name":"老王","id":1}
User{id=1, name=‘老王‘, password=‘老王‘}



一个简单的JSONDEMO

标签:ons   password   user   parse   set   out   return   ali   javabean   

原文地址:https://www.cnblogs.com/Han-God/p/12155284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!