Convert JSON data to objects

Source: Internet
Author: User

Convert the JSON data into an object and then call it directly with the object

1  PackageCom.lxj.register;2 3 ImportJava.io.BufferedReader;4 Importjava.io.IOException;5 ImportJava.io.InputStreamReader;6 Importjava.net.MalformedURLException;7 ImportJava.net.URL;8 9 ImportNet.sf.json.JSONObject;Ten  One  Public classDemo { A      Public Static voidMain (string[] args) { -         Try { -             //Create a URL to connect to the server theURL url =NewURL ("Http://192.168.18.249:8080/order/register"); -             Try { -                 //Create an input stream -BufferedReader br =NewBufferedReader (NewInputStreamReader (Url.openstream ())); +StringBuffer SB =NewStringBuffer (); - String St; +                 //start accepting data, first accept the St A                  while((st = Br.readline ())! =NULL){ at                     //and then splice it into SD. - Sb.append (ST); -                 } -                 //print out all the data - System.out.println (sb.tostring ()); -                 //Jsonobject.tobean () convert Jsonobject to register object in                 //Jsonobject.fromobject (sb.tostring () converts sb.tostring string to Jsonobject -                 //Register.class means to be converted into a Register . toRegister Register = (register) Jsonobject.tobean (Jsonobject.fromobject (sb.tostring ()), register.class); +                 //The server's data is stored in the Register object, as long as the register is called. -System.out.println ("msg:" +register.getmsg ()); theSystem.out.println ("RetCode:" +Register.getretcode ()); *System.out.println ("Name:" +register.getinfo (). GetName ()); $System.out.println ("Gender:" +register.getinfo (). Getsex ());Panax Notoginseng}Catch(IOException e) { -                 //TODO auto-generated Catch block the e.printstacktrace (); +             } A}Catch(malformedurlexception e) { the             //TODO auto-generated Catch block + e.printstacktrace (); -         } $     } $}
1  PackageCom.lxj.register;2 3  Public classRegister {4     PrivateString msg;5     PrivateString RetCode;6     Privateinfo info;7     8     //generate a set Get method9      PublicString getmsg () {Ten         returnmsg; One     } A  -  -      Public voidsetmsg (String msg) { the          This. msg =msg; -     } -  -  +      PublicString Getretcode () { -         returnRetCode; +     } A  at  -      Public voidSetretcode (String retCode) { -          This. RetCode =RetCode; -     } -  -  in      PublicInfo GetInfo () { -         returninfo; to     } +  -  the      Public voidSetInfo (Info info) { *          This. info =info; $     }Panax Notoginseng  -     //the info is created as an inner class, and the external class can call directly the      Public Static classInfo { +         PrivateString name; A         PrivateString sex; the          PublicString GetName () { +             returnname; -         } $          Public voidsetName (String name) { $              This. Name =name; -         } -          PublicString Getsex () { the             returnsex; -         }Wuyi          Public voidsetsex (String sex) { the              This. Sex =sex; -         } Wu          -     } About}

Convert JSON data to objects

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.