日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

delphi 解析json java_Delphi处理JSON格式数据

發布時間:2025/3/20 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 delphi 解析json java_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 := '';

//請求參數{"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.

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

/**

* The doGet method of the servlet.

*

* 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.

*

* 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.

*

* @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();

}

}

來自 “ ITPUB博客 ” ,鏈接:http://blog.itpub.net/10248702/viewspace-2133540/,如需轉載,請注明出處,否則將追究法律責任。

總結

以上是生活随笔為你收集整理的delphi 解析json java_Delphi处理JSON格式数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。