JSON conversion to Object

Source: Internet
Author: User
Tags goto

I've previously written an object into json,http://www.cnblogs.com/loger1995/p/6613488.html.

Now write a way to convert JSON into objects, and where to look!

Guide Package:

These packages through Baidu keyword: jsonobject must be 6 bags to find!

Create a new test class:

Package Com.loger.test;import Java.util.arraylist;import java.util.list;import org.junit.test;import Com.loger.bean.user;import net.sf.json.jsonarray;import net.sf.json.jsonobject;import net.sf.json.JsonConfig; Public classTestjson { Public Staticlist<user> list =NewArraylist<>(); Static{List.add (NewUser ("Tran Le"," +")); List.add (NewUser ("Zhang Zho-peng"," -")); }        /** Test Jsonarray go to List*/@SuppressWarnings ("unchecked") @Test Public voidTestjsonarraytoobject () {Jsonarray Jsonarray=jsonarray.fromobject (list); List<User> NewList =NewArraylist<>(); NewList= Jsonarray.tolist (Jsonarray,NewUser (),Newjsonconfig ()); System. out. println ("Test Jsonarray Goto List");  for(User u:newlist) {System. out. println (U.tostring ()); }    }        /** Test Jsonobject to Object*/@Test Public voidTestjsonobjecttoobject () {jsonobject jsonobject= Jsonobject.fromobject (NewUser ("Tran Le"," +")); User User=NewUser (); User= (User) Jsonobject.tobean (Jsonobject,NewUser (),Newjsonconfig ()); System. out. println ("test Jsonobject to Object"); System. out. println (user); }}

Note the parameters inside the Jsonarray.tolist () and Jsonobject.tobean () methods!

Operation Result:

Test Jsonarray Goto List
User [Name= Tran Le, age=21]
User [name= Zhang Zho-peng, age=20]


Test Jsonobject to Object
User [Name= Tran Le, age=21]

JSON conversion to Object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.