好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java中json与javaBean几种互转的讲解

一、java普通对象和json字符串的互转

java对象---->json

首先创建一个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

public class student {

   //姓名

   private string name;

   //年龄

   private string age;

   //住址

   private string address;

   public string getname() {

   return name;

   }

   public void setname(string name) {

   this .name = name;

   }

   public string getage() {

   return age;

   }

   public void setage(string age) {

   this .age = age;

   }

   public string getaddress() {

   return address;

   }

   public void setaddress(string address) {

   this .address = address;

   }

   @override

   public string tostring() {

   return "student [name=" + name + ", age=" + age + ", address="

   + address + "]" ;

   }

}

现在java对象转换为json形式:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public static void convertobject() {

     student stu= new student();

     stu.setname( "json" );

     stu.setage( "23" );

     stu.setaddress( "北京市西城区" );

     //1、使用jsonobject

     jsonobject json = jsonobject.fromobject(stu);

     //2、使用jsonarray

     jsonarray array=jsonarray.fromobject(stu);

     string strjson=json.tostring();

     string strarray=array.tostring();

     system.out.println( "strjson:" +strjson);

     system.out.println( "strarray:" +strarray);

}

定义了一个student的实体类,然后分别使用了jsonobject和jsonarray两种方式转化为json字符串,下面看打印的结果:

json-->javabean

上面说明了如何把java对象转化为json字符串,下面看如何把json字符串格式转化为java对象,

首先需要定义两种不同格式的字符串,需要使用\对双引号进行转义。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

public static void jsonstrtojava(){

     //定义两种不同格式的字符串

     string objectstr= "{\"name\":\"json\",\"age\":\"24\",\"address\":\"北京市西城区\"}" ;

     string arraystr= "[{\"name\":\"json\",\"age\":\"24\",\"address\":\"北京市西城区\"}]" ;

     //1、使用jsonobject

     jsonobject jsonobject=jsonobject.fromobject(objectstr);

     student stu=(student)jsonobject.tobean(jsonobject, student. class );

     //2、使用jsonarray

     jsonarray jsonarray=jsonarray.fromobject(arraystr);

     //获得jsonarray的第一个元素

     object o=jsonarray.get( 0 );

     jsonobject jsonobject2=jsonobject.fromobject(o);

     student stu2=(student)jsonobject.tobean(jsonobject2, student. class );

     system.out.println( "stu:" +stu);

     system.out.println( "stu2:" +stu2);

}

运行结果:

从上面的代码中可以看出,使用jsonobject可以轻松的把json格式的字符串转化为java对象,但是使用jsonarray就没那么容易了,因为它有[[]]符号,所以我们这里在获得了jsonarray的对象之后,取其第一个元素即我们需要的一个student的变形,然后使用jsonobject轻松获得。

二、list和json字符串的互转

下面将list转化为json字符串:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public static void convertlistobject() {

     student stu= new student();

     stu.setname( "json" );

     stu.setage( "23" );

     stu.setaddress( "北京市西城区" );

     student stu2= new student();

     stu2.setname( "json2" );

     stu2.setage( "23" );

     stu2.setaddress( "北京市西城区" );

     //注意如果是list多个对象比需要使用jsonarray

     jsonarray array=jsonarray.fromobject(stu);

     string strarray=array.tostring();

     system.out.println( "strarray:" +strarray);

}

运行结果为:

strarray:[{"address":"北京市西城区","name":"json","age":"23"},{"address":"北京市西城区","name":"json2","age":"23"}]

如果使用jsonobject进行转换会出现:

exception in thread "main" net.sf.json.jsonexception: 'object' is an array. use jsonarray instead

下面将json串转换为list

?

1

2

3

4

5

6

7

8

9

10

11

12

13

public static void jsontolist(){

     string arraystr= "[{\"name\":\"json\",\"age\":\"24\",\"address\":\"北京市西城区\"},{\"name\":\"json2\",\"age\":\"24\",\"address\":\"北京市西城区\"}]" ;

     //转化为list

     list<student> list2=(list<student>)jsonarray.tolist(jsonarray.fromobject(arraystr), student. class );

     for (student stu : list2) {

     system.out.println(stu);

     }

     //转化为数组

     student[] ss =(student[])jsonarray.toarray(jsonarray.fromobject(arraystr),student. class );

     for (student student : ss) {

     system.out.println(student);

     }

}

运行结果为:

三、map和json字符串的互转

map转化为json字符串

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public static void maptojson(){

     student stu= new student();

     stu.setname( "json" );

     stu.setage( "23" );

     stu.setaddress( "中国上海" );

     map<string,student> map= new hashmap<string,student>();

     map.put( "first" , stu);

     //1、jsonobject

     jsonobject mapobject=jsonobject.fromobject(map);

     system.out.println( "mapobject:" +mapobject.tostring());

     //2、jsonarray

     jsonarray maparray=jsonarray.fromobject(map);

     system.out.println( "maparray:" +maparray.tostring());

}

运行结果:

json字符串转化为map:

?

1

2

3

4

5

6

7

8

9

10

public static void jsontomap(){

   string strobject= "{\"first\":{\"address\":\"中国上海\",\"age\":\"23\",\"name\":\"json\"}}" ;

   //jsonobject

   jsonobject jsonobject=jsonobject.fromobject(strobject);

   map map= new hashmap();

   map.put( "first" , student. class );

   //使用了tobean方法,需要三个参数

   mybean my=(mybean)jsonobject.tobean(jsonobject, mybean. class , map);

   system.out.println(my.getfirst());

}

注意在转化为map的时候需要创建一个类,类里面需要有first属性跟json串里面的一样:

使用 tobean() 方法是传入了三个参数,第一个是jsonobject对象,第二个是mybean.class,第三个是一个map对象。通过mybean可以知道此类中要有一个first的属性,且其类型为student,要和map中的键和值类型对应,即,first对应键 first类型对应值的类型。

运行结果:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/u014320421/article/details/84315000

查看更多关于Java中json与javaBean几种互转的讲解的详细内容...

  阅读:35次