Detailed description and examples of XML and JSON data used in Java WeChat development

Source: Internet
Author: User
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:

  • Json-lib-2.4-jdk15.jar

  • Commons-logging-1.1.3.jar

  • Ezmorph-1.0.6.jar

  • Commons-lang-2.4.jar

  • Commons-collections.jar

  • Commons-beanutils-1.8.0.jar

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!

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.