1. Is it the only technology that WebService XML to pass data? The
can understand this. WebService is based on the SOAP protocol, the SOAP protocol is an XML-based text protocol. So webservice can be simply understood as an XML-based transmission technology, like HTML is a text-based transport protocol.
2.servlet is a generic term for a server-side Java program?
not. servlet is essentially an ordinary Java program, but he is a Java program that supports a standard that is the servlet specification. In addition to the servlet programs in Java to satisfy the servlet specification, Tomcat, JBoss, WebLogic and other Java server programs are also subject to the servlet specification.
3.webservice can I pass data through JSON? The
is not available. The webservice is XML-based.
4.json and XML are not side-by-side relationships? The
side-by-side relationship. JSON and XML are both descriptive data, is to achieve the same purpose of the different means, you can send a courier to choose the EMS also can be selected through. What is the concept of
5.http+json?
This is an applied thing. Everybody likes Http+json reason, mainly lies in the JSON JavaScript compatibility is better, the writing procedure is simple and convenient.
For example: The Courier sent to you is the way (the transmission protocol is HTTP), but a courier is packed in boxes (HTML), a courier is packaged in a bag (JSON), both can send you a courier, but there are cases convenient, some cases convenient bag.
6. I have now released a web service by Tomcat+axis2, which calculates two integers and can be accessed through the browser. Is this a webservice?
Yes.
7. I set up a Poco folder under the tomcat/webappps/axis2/web-info/of the Axis2, put the Java program of class type in, and the general server is deployed like this?
not. In general, the Web project is exported as a war package, and then the war package is copied to the tomcat/webappps/directory. The
WebService project is essentially an ordinary java web project, the same principle. on-line or in the tutorial more hot release WebService, I think there is no practical use, the general re-release WebService involves redeploying or restarting the server.
Much of the use of interface access and data transmission is using JSON objects to manipulate formatted data: Using JSON strings on the server side to pass data and parsing the received data in the Web front-end or on the Android client with JSON.
First, the use of JSON in Java requires the introduction of the Org.json package (click here to download the corresponding JAR package!). ), and introduce the appropriate JSON class in the program:
Import Org.json.JSONArray; Import org.json.JSONException; Import Org.json.JSONObject;
Second, in the server-side servlet class, you can use the following methods to collect data and generate the appropriate JSON string
1//Declare a hash object and add data 2 Map params = new HashMap (); 3 4 params.put ("username", username); 5 params.put ("User_json", user) ; 6 7//Declare the Jsonarray object and enter the JSON string 8 jsonarray array = jsonarray.fromobject (params); 9 put.println (Array.tostring ());
JSON strings can be parsed directly in the Web front-end via JavaScript, and in the case of Android clients, a JSON class is used to parse the string:
1//@description: Parse the data and data objects contained in the string according to the received JSON string 2 3//Received JSON string 4 string result = "[{\" username\ ": \" Your name\ ", \ "user_json\": {\ "username\": \ "Your name\", \ "nickname\": \ "Your Nickname\"}}] "; 5 6//Generate JSON object based on string 7 Jsonarray resultarray = new Jsonarray (result); 8 Jsonobject resultobj = Resultarray.optjsonobjec T (0); 9 10//Get data item one by one String username = resultobj.getstring ("username"); 12 13//Get data object Jsonobject user = Resultobj.getjsonobjec T ("User_json"); String nickname = user.getstring ("nickname");
In fact, the main concern is the conversion between the following centralized data types:
1. String conversion to JSON object
1 Import Org.json.JSONArray; 2 Import org.json.JSONException; 3 Import Org.json.JSONObject; 4 5//Do not forget to add the JSON package Oh! 6 public class Stringtojson {7 public static void Main (string[] args) throws jsonexception{8 9 Syst EM.OUT.PRINTLN ("abc"); 10//define JSON string one by one string jsonstr = "{\" id\ ": 2," + "\" title\ ": \" JSON title\ "," + "\" config\ ": {" +14 "\" width\ ": +15" \ "h Eight\ ": +," +16 "}, \" Data\ ": [" +17 "\" Java\ ", \" javascript\ ", \" Php\ "" +18 "]}"; 19 20//Convert into Jsonobject object Jsonobject jsonobj = new Jsonobject (JSONSTR); 22 2 3//Get data from Jsonobject object JavaBean bean = new JavaBean (); 25 26//Get int type data according to the attribute name; 27 Bean.setid (Jsonobj.getint ("id")); 28 29//Get String data according to the attribute name; Bean.settitle (jsonobj.getstring ("title")); 31 32Gets the Jsonobject class jsonobject config = jsonobj.getjsonobject ("config") based on the attribute name; Bean.setwidth (Config.getint ( "width"); Bean.setheight (Config.getint ("height")); 36 37//Get Jsonarray array by attribute name. Jsonarra Y data = Jsonobj.getjsonarray ("Data"), (int index = 0, length = data.length (), index < length; index++) {4 0//Here in Org.json.JSONArray object actually did not find ToArray method, ask you to give a solution ah! Bean.setlanguages (string[]);}44}45-Class javabean{47 private int id; 4 8 private String title;49 private int width;50 private int height;51 private string[] languages;52 53 This omits the setup and accessor 54}
2. JSON object converted to string string
1 public static void main (string[] args) throws Jsonexception {2 3 //Create Jsonobject Object 4 jsonobject json = new J Sonobject (); 5 6 //Add data to JSON 7 json.put ("username", "Wanglihong"), 8 json.put ("height", 12.5), 9 json.put ( "Age", "ten") ; Create a Jsonarray array and add the JSON to the array jsonarray array = new Jsonarray (); Array.put ( JSON);/ /Convert to string jsonstr = array.tostring (); System.out.println ( JSONSTR);
The resulting output is: [{"username": "Wanglihong", "height": 12.5, "Age": 24}]
3. set conversion to Jsonarray object
public static void Main (string[] args) throws jsonexception{ //Initialize ArrayList collection and add data list<string> List = New Arraylist<string> (); List.add ("username"); List.add ("Age"); List.add ("Sex"); Initializes the HashMap collection and adds an array of map map = new HashMap (); Map.put ("BookName", "CSS3 Combat"); Map.put ("Price", 69.0); Initializes the Jsonarray object and adds the data jsonarray array = new Jsonarray (); Array.put (list); Array.put (map); The resulting JSON string is: [[' Username ', ' age ', ' sex '],{' price ': ' bookname ': ' CSS3 Combat '}] }
A simple example of reading and writing JSON data in person:
Read:
1 Import java.io.FileNotFoundException; 2 Import Java.io.FileReader; 3 4 Import Com.google.gson.JsonArray; 5 Import com.google.gson.JsonIOException; 6 Import Com.google.gson.JsonObject; 7 Import Com.google.gson.JsonParser; 8 Import com.google.gson.JsonSyntaxException; 9 public class Json_read {One to ten public static void Main (string[] args) {try {J] Sonparser parser = new Jsonparser (); Jsonobject object = (jsonobject) parser.parse (New FileReader ("Test.json "); System.out.println (" cat= "+object.get (" Cat "). getasstring ()); System.out.println (" pop= "+obj Ect.get ("Pop"). Getasboolean ()); Jsonarray array = Object.get ("languages"). Getasjsonarray (); 21 for (int i=0;i<array.size (); i++) {System.out.println ("--------"); Jsono Bject subobject = Array.get (i). Getasjsonobject (); System.out.println ("Id=" +subobject.get ("id"). GetasiNT ()); System.out.println ("Name=" +subobject.get ("name"). getasstring ()); System.out.pri Ntln ("ide=" +subobject.get ("IDE"). getasstring ()); catch (Json IOException e) {e.printstacktrace (); [+] catch (jsonsyntaxexception e) {E.printstack Trace (), filenotfoundexception} catch (e) {e.printstacktrace (); 36}37}38 39}40
Write:
Import Com.google.gson.jsonarray;import Com.google.gson.jsonobject;public class Json_write {public static void main (St Ring[] args) {Jsonobject object = new Jsonobject ();//Overall Jsonobject container Object.addproperty ("Cat", "it"); Jsonarray array = new Jsonarray (); Jsonobject lan1 = new Jsonobject (); Lan1.addproperty ("id", 1); Lan1.addproperty ("name", "Java"); Lan1.addproperty ("IDE", "Eclipse"); Array.add (LAN1); Jsonobject lan2 = new Jsonobject (); Lan2.addproperty ("id", 2); Lan2.addproperty ("name", "SWITF"); Lan2.addproperty ("IDE", "XCode"); Array.add (LAN2); Jsonobject lan3 = new Jsonobject (); Lan3.addproperty ("id", 3); Lan3.addproperty ("name", "C #"); Lan3.addproperty ("IDE", "Visual Studio"); Array.add (LAN3); Object.add ("Languages", array); Object.addproperty ("Pop", true); SYSTEM.OUT.PRINTLN (oBject.tostring ()); }}
Test.json
1 {2 "cat": "It", 3 "languages": [4 {"id": 1, "IDE": "Eclipse", "name": "Java"},5 {"id": 2, "IDE": "XCode" , "name": "Swift"},6 {"id": 3, "IDE": "Visual Studio", "Name": "C #"}7 ],8 "Pop": True9}
Transferred from: https://www.cnblogs.com/UniqueColor/p/5724795.html
Using JSON for data passing in Java