Json_ 0 Base _006_ Converts a JSON-formatted string into a Java object

Source: Internet
Author: User

Demand:
Converts a JSON-formatted string into a Java object.

Apply this technique to get a Java-corresponding object from a JSON object string format.
Jsonobject is a "name.values" collection,
The value portion (string) corresponding to key is obtained by the Get (key) method.
A Jsonobject object can be obtained by Getjsonobject (key).
A Jsonarray object can be obtained by Getjsonarray (key).

The jar package that needs to be imported:

Write the PO (Bean) Class:

Package Com.west.webcourse.po;/** No. 01 Step: Write The Bean class, * next Com.west.webcourse.servlet.JavaBeanToJOSNString.java*/ Public classPersoninfopo {PrivateString name; Private intAge ; PrivateString sex;  PublicPersoninfopo () {} Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicString Getsex () {returnsex; }     Public voidsetsex (String sex) { This. Sex =sex; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }    }

To write a servlet:

Package Com.west.webcourse.servlet;import Java.io.ioexception;import java.io.printwriter;import Java.util.arraylist;import java.util.hashmap;import java.util.hashset;import Java.util.list;import java.util.Map; Import Java.util.set;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.west.webcourse.po.personinfopo;import Net.sf.json.jsonarray;import Net.sf.json.JSONObject; Public classJavabeantojosnstring extends HttpServlet {personinfopo pif=NewPersoninfopo (); Personinfopo pif01=NewPersoninfopo (); /** No. 03 step: Rewrite the Doget () method, Next: Test*/@Overrideprotected voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException { Response.setcontenttype ("Text/html;charset=utf-8"); PrintWriter out=Response.getwriter (); System. out. println ("Conversion");  out. Print ("the converted JSON string:<br/>"); /** Send a JSON-formatted string to the browser*/Getjsonstringtobean ();  out. Flush ();  out. Close (); }        /** No. 02 Step: Convert the JSON type string to: Po class*/     Public voidGetjsonstringtobean () {String jsonstring="{\ "name\": \ "zhang3\", \ "sex\": \ "nan\", \ "age\": \ "30\"}"; Jsonobject Jsonobject=Jsonobject.fromobject (jsonstring); Pif01= (Personinfopo) Jsonobject.tobean (Jsonobject,personinfopo.class); System. out. println ("Name:"+pif01.getname () +": Gender:"+pif01.getsex () +": Age:"+pif01.getage ()); }}

To write a test class:

Package Com.west.webcourse.servlet;/** No. 04 Step: Test * Next: Configure Web. XML*/import Org.junit.beforeclass;import org.junit.Test; Public classJavabeantojosnstringtest {Staticjavabeantojosnstring JS; @BeforeClass Public Static voidSetupbeforeclass () throws Exception {JS=Newjavabeantojosnstring (); } @Test Public voidJsonstringtobean () {Js.getjsonstringtobean (); }}

Json_ 0 Base _006_ Converts a JSON-formatted string into a Java 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.