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. <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();
}
}
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. <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();
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10248702/viewspace-2133540/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 處理json格式的資料JSON
- Python資料處理(一):處理 JSON、XML、CSV 三種格式資料PythonJSONXML
- jQuery處理json格式資料程式碼例項jQueryJSON
- 原生ajax處理json格式資料程式碼例項JSON
- 處理JSON資料JSON
- Delphi資料壓縮處理(1) (轉)
- Delphi資料壓縮處理(2) (轉)
- flutter json資料處理FlutterJSON
- Hive處理Json資料HiveJSON
- spark處理json資料DemoSparkJSON
- JSON 資料格式JSON
- web api 返回Json資料中人格式帶T處理WebAPIJSON
- JSON資料傳輸大法第一式——用OADate處理日期格式JSON
- 資料匯入與預處理實驗二---json格式檔案轉換JSON
- 為什麼eval()處理json格式資料需要多加一個小括號JSON
- 開發工具-scala處理json格式利器-json4sJSON
- C#中處理JSON資料的方式C#JSON
- JSON資料格式的使用JSON
- php操作JSON格式資料PHPJSON
- URL及日期等特殊資料格式處理-JSON框架Jackson精解第2篇JSON框架
- JMeter中對於Json資料的處理方法JMeterJSON
- flutter json_annotation和json_serializable處理json資料序列化FlutterJSON
- 資料型別和Json格式資料型別JSON
- Delphi處理TWebBrowser的Close事件Web事件
- java 如何簡單快速處理 json 中的資料JavaJSON
- C++實現對Json資料的友好處理C++JSON
- delphi基於資料模型(data-model)JSON序列模型JSON
- iview Tree資料格式問題,無限遞迴樹處理資料View遞迴
- JSON資料交換格式有幾種?JSON
- springMVC傳遞JSON格式資料SpringMVCJSON
- Java與Json資料格式轉換JavaJSON
- 資料格式之戰:JSON vs XMLJSONXML
- 第四章 Caché JSON 處理資料型別JSON資料型別
- Python資料處理(二):處理 Excel 資料PythonExcel
- 向Solr資料集提交Json格式資料(Scala,Post)SolrJSON
- 資料處理
- ajax如何處理伺服器返回的3種資料格式伺服器
- 將任意格式轉換為JSON資料格式的工具類JSON