Delphi怎么處理JSON格式數(shù)據(jù)

本篇內(nèi)容主要講解“Delphi怎么處理JSON格式數(shù)據(jù)”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Delphi怎么處理JSON格式數(shù)據(jù)”吧!

創(chuàng)新互聯(lián)建站擁有10多年成都網(wǎng)站建設(shè)工作經(jīng)驗(yàn),為各大企業(yè)提供成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)服務(wù),對(duì)于網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、成都App定制開發(fā)、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、程序開發(fā)、網(wǎng)站優(yōu)化(SEO優(yōu)化)、微網(wǎng)站、國(guó)際域名空間等,憑借多年來(lái)在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了很多網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開發(fā)、設(shè)計(jì)、營(yíng)銷、管理等網(wǎng)站化運(yùn)作于一體,具備承接各種規(guī)模類型的網(wǎng)站建設(shè)項(xiàng)目的能力。

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


uses SuperObject,uLkJSON,strprocess;
//POST JSON數(shù)據(jù)格式的請(qǐng)求


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


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


   JsonSend := TlkJSONobject.Create;//必須先Create一個(gè)對(duì)象
   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;
  // 錯(cuò)誤的JSON數(shù)據(jù)格式,為什么會(huì)多了[] : [{"code":"0","message":"success","object":{"bandid":"000001","coin":"5"}}]
  // 返回正確的JSON數(shù)據(jù)格式 {"code":"0","message":"success","object":{"bandid":"000001","coin":"5"}}
    
    JsonReceived := TlkJSON.ParseText(TrimRightChar(TrimLeftChar(trim(strResp),'['),']')) as TlkJSONobject;
    //Jstart.field 為jbase時(shí),
    strBandID := vartostr(JsonReceived.Field['object'].Field['bandid'].Value);
    memo3.Lines.Clear;
    memo3.Lines.add(strBandID);
    //Jstart.field 有子數(shù)據(jù)為jslist時(shí)
    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(); 
}


}

到此,相信大家對(duì)“Delphi怎么處理JSON格式數(shù)據(jù)”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

本文題目:Delphi怎么處理JSON格式數(shù)據(jù)
鏈接分享:http://muchs.cn/article44/ipiphe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、網(wǎng)站改版App開發(fā)、Google、網(wǎng)站策劃網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司