Delphi怎么处理JSON格式数据

发布时间:2021-08-15 20:15:29 作者:chen
来源:亿速云 阅读:856

本篇内容主要讲解“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 {


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


}

到此,相信大家对“Delphi怎么处理JSON格式数据”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. 如何在Delphi中访问DBF数据库
  2. SQLMAP注入json格式数据

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

delphi json

上一篇:Storm和Hadoop哪个快

下一篇:cell怎么实现旋转动画效果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》