本篇内容主要讲解“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 {
public ServletDelphi() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().println("Hello Servlet Delphi!");
}
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());
}
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格式数据”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!