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