十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本篇内容主要讲解“Delphi怎么处理JSON格式数据”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Delphi怎么处理JSON格式数据”吧!
创新互联建站拥有10多年成都网站建设工作经验,为各大企业提供成都做网站、网站设计服务,对于网页设计、PC网站建设(电脑版网站建设)、成都App定制开发、wap网站建设(手机版网站建设)、程序开发、网站优化(SEO优化)、微网站、国际域名空间等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了很多网站制作、网站设计、网络营销经验,集策划、开发、设计、营销、管理等网站化运作于一体,具备承接各种规模类型的网站建设项目的能力。
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 {
/**
* 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();
}
}
到此,相信大家对“Delphi怎么处理JSON格式数据”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!