jsonobject 与 jsonarray区别
jsonobject:
1
2
3
4
5
|
{
"area" : "武汉" ,
"name" : "张三" ,
"age" : 25
}
|
jsonarray:
1
2
3
4
5
6
7
8
9
10
|
[{
“area”: “武汉”,
“name”: “张三”,
“age”: 25
},
{
“area”: “深圳”,
“name”: “李四”,
“age”: 22
}]
|
通俗来讲 jsonobject 是对象的json形式 jsonarry 是对象集合的json形式。
json 与javabean互转
json用阿里的fastjson 包
用例java对象
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
|
public class user {
protected long id;
protected string account;
protected string password;
protected string name;
protected boolean gender;
protected string telephone;
@override
public string tostring() {
return "user{" +
"id=" + id +
", account='" + account + '\ '' +
", password='" + password + '\ '' +
", name='" + name + '\ '' +
", gender=" + gender +
", telephone='" + telephone + '\ '' +
'}' ;
}
public boolean isgender() {
return gender;
}
public void setgender( boolean gender) {
this .gender = gender;
}
public string gettelephone() {
return telephone;
}
public void settelephone(string telephone) {
this .telephone = telephone;
}
public string getname() {
return name;
}
public void setname(string name) {
this .name = name;
}
public long getid() {
return id;
}
public void setid( long id) {
this .id = id;
}
public string getaccount() {
return account;
}
public void setaccount(string account) {
this .account = account;
}
public string getpassword() {
return password;
}
public void setpassword(string password) {
this .password = password;
}
}
|
1、javabean转json
方法一:通过java对象转成string再转成jsonobject
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.handoop.gms.utils;
import com.alibaba.fastjson.jsonobject;
import com.handoop.gms.domain.user;
public class testmain {
public static void main(string []args){
//先通过构造函数初始化一个对象
user user= new user(( long ) 1 , "admin" , "admin" , "张三" , true , "123456" );
//先将java对象转为string类型
string jsonstring= jsonobject.tojsonstring(user);
//再将string类型转为jsonobject
jsonobject jsonobject=jsonobject.parseobject(jsonstring);
system.out.println(jsonobject);
//转为jsonobject后就可以随时根据键值获取他的元素了
system.out.println(jsonobject.get( "password" ));
}
}
|
运行结果
方法2:java对象直接转json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.handoop.gms.utils;
import com.alibaba.fastjson.jsonarray;
import com.alibaba.fastjson.jsonobject;
import com.handoop.gms.domain.user;
public class testmain {
public static void main(string []args){
//先通过构造函数初始化一个对象
user user= new user(( long ) 1 , "admin" , "admin" , "张三" , true , "123456" );
jsonobject jsonobject= (jsonobject) jsonobject.tojson(user);
system.out.println(jsonobject);
}
}
|
运行结果
json字符串转jsonobeject
1
2
3
4
5
6
7
|
public class testmain {
public static void main(string []args){
string str= "{\"password\":\"admin\",\"gender\":true,\"name\":\"张三\",\"telephone\":\"123456\",\"id\":1,\"account\":\"admin\"}" ;
jsonobject jsonobject=jsonobject.parseobject(str);
system.out.println( "account: " +jsonobject.get( "account" )+ "---" + "paasword: " +jsonobject.get( "password" ));
}
}
|
运行结果
3.jsonstring 转jsonarray
1
2
3
4
5
6
7
8
9
10
11
|
public class testmain {
public static void main(string []args){
string str= "{\"data\":[{\"password\":\"admin\",\"gender\":true,\"name\":\"张三\",\"telephone\":\"123456\",\"id\":1,\"account\":\"admin\"}]}" ;
//先转成jsonobject
jsonobject jsonobject=jsonobject.parseobject(str);
//再将jsonobject中数组类型数据取出转成jsonarray
jsonarray jsonarray=jsonobject.getjsonarray( "data" );
system.out.println(jsonarray.get( 0 ));
}
}
|
运行结果
4.json字符串转java对象
1
2
3
4
|
string str= "{\"password\":\"admin\",\"gender\":true,\"name\":\"张三\",\"telephone\":\"123456\",\"id\":1,\"account\":\"admin\"}" ;
// 前面是json字符串 后面是java对象类型
user user=jsonobject.parseobject(str,user. class );
system.out.println( "account: " +user.getaccount()+ "---" + "paasword: " +user.getpassword());
|
输出结果
到此这篇关于常用json与javabean互转的方法实现的文章就介绍到这了,更多相关json与javabean互转内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/zyz_YZJ/article/details/115443741