亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

Delphi怎么處理JSON格式數據

發布時間:2021-08-15 20:15:29 來源:億速云 閱讀:862 作者:chen 欄目:web開發

本篇內容主要講解“Delphi怎么處理JSON格式數據”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Delphi怎么處理JSON格式數據”吧!

1 下載/安裝組件uLkJSON.pas
2 下載/安裝組件strprocess.pas


uses SuperObject,uLkJSON,strprocess;
//POST JSON數據格式的請求


procedure TForm1.btnPostRequestClick(Sender: TObject);
var
  Url,strBandID,strShopID,str3,str4,strCoin: string;//請求地址
  strReqJson: TStringStream;
  JsonReceived,JsonSend:  TlkJSONobject;
  strResp : string;
begin
 //請求地址
  Url := 'http://localhost:8080/testdelphi/servlet/ServletDelphi';
  //請求參數{"bandid":"手環ID","shopid":"場地ID","sign":"參數簽名"}
  //創建一個包含JSON數據的變量,這種格式有問題嗎?


   strBandID  := '000001';
   strShopID  := '000001';


   JsonSend := TlkJSONobject.Create;//必須先Create一個對象
   JsonSend.Add('bandid',strBandID);
   JsonSend.Add('shopid',strShopID);
   JsonSend.Add('sign',getSignature(strBandID+strShopID));


    strReqJson := TStringStream.Create(TlkJSON.GenerateText(JsonSend));
    memo1.Lines.Clear;
    memo1.Lines.add(strReqJson.DataString);
    strReqJson.Position := 0;
  try
    IdHTTP1.Request.ContentType := 'application/json';
    strResp := IdHTTP1.Post(URL, strReqJson);
    memo2.Lines.Clear;
    Memo2.Lines.Text :=strResp;
  // 錯誤的JSON數據格式,為什么會多了[] : [{"code":"0","message":"success","object":{"bandid":"000001","coin":"5"}}]
  // 返回正確的JSON數據格式 {"code":"0","message":"success","object":{"bandid":"000001","coin":"5"}}
    
    JsonReceived := TlkJSON.ParseText(TrimRightChar(TrimLeftChar(trim(strResp),'['),']')) as TlkJSONobject;
    //Jstart.field 為jbase時,
    strBandID := vartostr(JsonReceived.Field['object'].Field['bandid'].Value);
    memo3.Lines.Clear;
    memo3.Lines.add(strBandID);
    //Jstart.field 有子數據為jslist時
    strCoin := vartostr(JsonReceived.Field['object'].Field['coin'].Value);
    memo3.Lines.add(strCoin);


             
  finally
    JsonSend.Free;
    JsonReceived.Free;
  end;
end;






文件ServletDelphi.java


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;




public class ServletDelphi extends HttpServlet {


/**
* Constructor of the object.
*/
public ServletDelphi() {
super();
}


/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


response.getWriter().println("Hello Servlet Delphi!");
}


/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


String strJson = inputStream2String(request.getInputStream());
//System.out.println("receive json:"+json);
//response.getWriter().println(json);


JSONObject subJsonObj = new JSONObject();
subJsonObj.put("bandid", "000001");
subJsonObj.put("coin", "5");

JSONObject responseJsonObj = new JSONObject();
responseJsonObj.put("code", "0");
responseJsonObj.put("message", "success");
responseJsonObj.put("object", subJsonObj);


JSONArray array = new JSONArray();
array.add(responseJsonObj);
//System.out.println("return JSON: " + array.toString());
response.getWriter().println(array.toString());
       

}


/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}


public static String inputStream2String (InputStream in) throws IOException { 
StringBuffer out = new StringBuffer(); 
byte[] b = new byte[4096]; 
for (int n; (n = in.read(b)) != -1;) { 
out.append(new String(b, 0, n)); 

return out.toString(); 
}


}

到此,相信大家對“Delphi怎么處理JSON格式數據”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

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

AI

修武县| 靖远县| 余姚市| 青阳县| 乌鲁木齐市| 洪湖市| 枣强县| 滦平县| 江安县| 淮滨县| 陆河县| 临武县| 裕民县| 新闻| 象州县| 双江| 车致| 安岳县| 麻城市| 罗定市| 达孜县| 锡林郭勒盟| 竹山县| 沐川县| 和田县| 视频| 漠河县| 道真| 营山县| 桦甸市| 云梦县| 平乐县| 四子王旗| 天等县| 寻乌县| 龙口市| 丰原市| 揭阳市| 永嘉县| 红安县| 三穗县|