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
- flutter json資料處理FlutterJSON
- Hive處理Json資料HiveJSON
- spark處理json資料DemoSparkJSON
- JSON資料傳輸大法第一式——用OADate處理日期格式JSON
- web api 返回Json資料中人格式帶T處理WebAPIJSON
- 資料匯入與預處理實驗二---json格式檔案轉換JSON
- 開發工具-scala處理json格式利器-json4sJSON
- URL及日期等特殊資料格式處理-JSON框架Jackson精解第2篇JSON框架
- C#中處理JSON資料的方式C#JSON
- flutter json_annotation和json_serializable處理json資料序列化FlutterJSON
- delphi基於資料模型(data-model)JSON序列模型JSON
- java 如何簡單快速處理 json 中的資料JavaJSON
- C++實現對Json資料的友好處理C++JSON
- Python處理JSONPythonJSON
- IDocList/IDocDict JSON for Delphi and FPCJSON
- 使用Java處理JSON結構化資料 -Advanced Web MachineryJavaJSONWebMac
- 第四章 Caché JSON 處理資料型別JSON資料型別
- iview Tree資料格式問題,無限遞迴樹處理資料View遞迴
- Python資料處理(二):處理 Excel 資料PythonExcel
- JSON資料交換格式有幾種?JSON
- Delphi原生JSON框架(一) TJsonValueJSON框架
- Delphi原生JSON框架(二)TJsonArrayJSON框架
- 資料處理
- MapReduce中對大資料處理最合適的資料格式是什麼?大資料
- PostgreSQL處理JSON入門SQLJSON
- flutter demo (三):json處理FlutterJSON
- golang json處理問題GolangJSON
- 拿來即用:用C+JS結構來處理JSON資料JSON
- json_encode的資料格式化的兩種格式[]和{}JSON
- 探索多種資料格式:JSON、YAML、XML、CSV等資料格式詳解與比較JSONYAMLXML
- json字串轉義格式化後再轉換處理demo StringEscapeUtils.unescapeJavaJSON字串Java
- java localdate日期格式處理JavaLDA
- Vue格式化處理Vue
- 海量資料處理
- python 處理資料Python
- springmvc 資料處理SpringMVC
- Panda資料處理