Json_ 0 Base _007_ Converts a JSON-formatted "array" string to a Java object "array"

Source: Internet
Author: User


Converts a JSON-formatted "array" string to a Java object "array".

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).

To import the required jar packages:

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 class:

Package Com.west.webcourse.servlet;import Java.io.ioexception;import java.io.printwriter;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; 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*/Getjsonstringtobeans ();  out. Flush ();  out. Close (); }    /** No. 02 Step: Convert the JSON type string to: Po class*/     Public voidGetjsonstringtobeans () {String jsonstring="[{\ "name\": \ "zhang3\", \ "sex\": \ "nan\", \ "age\": \ "30\"},{\ "name\": \ "zhang4\", \ "sex\": \ "nan4\", \ "age\": \ "304\ "}]"; System. out. println (jsonstring); /*2.1: Convert to JSON array*/Jsonarray alljsons=Jsonarray.fromobject (jsonstring); /*2.2: JSON array converted to Personinfopo array*/personinfopo[] Allbeans= (personinfopo[]) Alljsons.toarray (Alljsons, Personinfopo.class);  for(ObjectObject: Allbeans) {pif01= (Personinfopo)Object; 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.getjsonstringtobeans (); }}

Write the PO (Bean) Class:

Json_ 0 Base _007_ Converts a JSon-formatted "array" string to a Java object "array"

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.