溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java中使用XML格式和JSON格式數(shù)據(jù)的示例

發(fā)布時間:2021-01-28 09:50:08 來源:億速云 閱讀:187 作者:小新 欄目:移動開發(fā)

小編給大家分享一下Java中使用XML格式和JSON格式數(shù)據(jù)的示例,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

Java微信開發(fā)中使用XML格式和JSON格式數(shù)據(jù)的示例

注意一下json-lib所需要的jar包

XML微信XML消息model定義:

package cn.wx.server;
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
 
/**
 * @title cn.wx.serverXMLMsg.java
 * @todo TODO
 * @author lpe234
 * @time 2014年5月21日下午2:13:27
 */
public class XMLMsg {
//普通消息基本變量
 String ToUserName;
 String FromUserName;
 String CreateTime;
 String MsgType;
 String Content;
 String MsgId;
//事件推送變量
 String Event;
//自定義菜單項
 String EventKey;
  
 
 public String getEventKey() {
  return EventKey;
 }
 
 public void setEventKey(String eventKey) {
  EventKey = eventKey;
 }
 
 public XMLMsg(String str) throws DocumentException {
  Document doc = DocumentHelper.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
這里我們使用json-lib,注意一下需要以下幾個jar包的支持:

  • 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

以下是簡單的AccessToken類,返回String類型的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 {
 
 /**
  * 根據(jù)注冊信息,獲得的參數(shù),提交get請求,獲得accessTkoen
  * @author lpe234
  * @time 2014-5-21 00:52:15
  */
 String appID = "XXXXXXXXXXXXXX";
 String appsecret = "XXXXXXXXXXXXXXXXX";//微信服務號或者申請測試賬號的訂閱號才有。。。
 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);
/** 測試
* public static void main(String[] args) {
*  AccessToken as = new AccessToken();
*  System.out.println(as.get());
* }
*/
 //返回String類型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;
 }
 
 // 獲取wx服務器返回JSON數(shù)據(jù),private內部調用
 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;
 }
}

看完了這篇文章,相信你對“Java中使用XML格式和JSON格式數(shù)據(jù)的示例”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI