This article describes examples of using XML and JSON data in Java development. pay attention to the jar packages required by json-lib, for more information about how to use the XML format and JSON format data in Java development, pay attention to the jar package required by json-lib, for more information, see
XMLXML message model definition:
Package cn. wx. server; import org. dom4j. document; import org. dom4j. extends entexception; import org. dom4j. export enthelper; import org. dom4j. element;/*** @ title cn. wx. serverXMLMsg. java * @ todo TODO * @ author lpe234 * @ time May 21, 2014 2:13:27 */public class XMLMsg {// basic variable of common messages String ToUserName; String FromUserName; String CreateTime; String MsgType; string Content; String MsgId; // Event push variable String Event; // custom menu item String EventKey; public String getEventKey () {return EventKey;} public void setEventKey (String eventKey) {EventKey = eventKey;} public XMLMsg (String str) throws condition entexception {Document doc = incluenthelper. parseText (str); Element root = doc. getRootElement (); this. toUserName = root. elementText ("ToUserName"); this. fromUserName = root. elementText ("FromUserName"); this. createTime = root. elementText ("CreateTime"); this. msgType = root. elementText ("MsgType"); this. content = root. elementText ("Content"); this. msgId = root. elementText ("MsgId"); this. event = root. elementText ("Event"); this. eventKey = root. elementText ("EventKey");} public String getEvent () {return Event;} public void setEvent (String event) {Event = event;} public String getToUserName () {return ToUserName;} public void setToUserName (String toUserName) {ToUserName = toUserName;} public String getFromUserName () {return FromUserName;} public void setFromUserName (String fromUserName) {FromUserName = fromUserName;} public String getCreateTime () {return CreateTime;} public void setCreateTime (String createTime) {CreateTime = createTime;} public String getMsgType () {return MsgType ;} public void setMsgType (String msgType) {MsgType = msgType;} public String getContent () {return Content;} public void setContent (String content) {Content = content ;} public String getMsgId () {return MsgId;} public void setMsgId (String msgId) {MsgId = msgId ;}}
JSON
Here we use json-lib. Note that the following jar packages are required:
The following is a simple AccessToken class that returns the String-type access_token.
Package cn. wx. server; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStreamReader; import java.net. malformedURLException; import java.net. URL; import java.net. URLConnection; import net. sf. json. JSONObject; public class AccessToken {/*** submit a get request based on the parameters obtained in the registration information, obtain accessTkoen * @ author lpe234 * @ time 00:52:15 */String appID = "XXXXXXXXXXXXXX"; String appsecret = "X... String preUrl =" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s "; String tempUrl = String. format (preUrl, appID, appsecret);/** test * public static void main (String [] args) {* AccessToken as = new AccessToken (); * System. out. println (. get (); *} * // returns the String type access_token public String get () {String temp = null; temp = getJSON (); JSONObject j = JSONObject. fromObject (temp); temp = j. getString ("access_token"); // System. out. println (temp); return temp;} // Get the JSON data returned by the wx server. private calls private String getJSON () {String temp = null; try {URL url = new URL (tempUrl); URLConnection conn = url. openConnection (); InputStreamReader isr = new InputStreamReader (conn. getInputStream (); BufferedReader br = new BufferedReader (isr); temp = br. readLine ();} catch (MalformedURLException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} // System. out. println (temp); return temp ;}}
This is basically the case.
The above is a detailed description of the data in XML and JSON formats used in Java development and the details of the instance. For more information, see other related articles on php Chinese network!