简体   繁体   English

使用Freepascal \\ Lazarus JSON库

[英]Using Freepascal\Lazarus JSON Libraries

I'm hoping for a bit of a "simpletons" demo\\explanation for using Lazarus\\Freepascal JSON parsing. 我希望对使用Lazarus \\ Freepascal JSON解析进行一些“简单的”演示/解释。 I've asked a question here but all the replies are "read this" and none of them are really helping me get a grasp because the examples are bit too in-depth and I'm seeking a very simple example to help me understand how it works. 我在这里问了一个问题但是所有的答复都是“读这篇文章”,它们都没有真正帮助我理解,因为这些示例过于深入,我正在寻找一个非常简单的示例来帮助我理解有用。

In brief, my program reads an untyped binary file in chunks of 4096 bytes. 简而言之,我的程序以4096字节的块大小读取未类型化的二进制文件。 The raw data then gets converted to ASCII and stored in a string. 然后将原始数据转换为ASCII并存储在字符串中。 It then goes through the variable looking for certain patterns, which, it turned out, are JSON data structures. 然后,它遍历变量以查找某些模式,事实证明,这些模式是JSON数据结构。 I've currently coded the parsing the hard way using Pos and ExtractANSIString etc. But I'vesince learnt that there are JSON libraries for Lazarus & FPC, namely fcl-json, fpjson, jsonparser, jsonscanner etc. 我目前已经使用Pos和ExtractANSIString等对解析进行了编码。但是我已经了解到Lazarus和FPC有JSON库,即fcl-json,fpjson,jsonparser,jsonscanner等。

https://bitbucket.org/reiniero/fpctwit/src http://fossies.org/unix/misc/fpcbuild-2.6.0.tar.gz:a/fpcbuild-2.6.0/fpcsrc/packages/fcl-json/src/ http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/packages/fcl-json/examples/ https://bitbucket.org/reiniero/fpctwit/src http://fossies.org/unix/misc/fpcbuild-2.6.0.tar.gz:a/fpcbuild-2.6.0/fpcsrc/packages/fcl-json / src / http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/packages/fcl-json/examples/

However, I still can't quite work out HOW I read my string variable and parse it for JSON data and then access those JSON structures. 但是,我仍然不太清楚如何读取我的字符串变量并解析为JSON数据,然后访问那些JSON结构。

Can anyone give me a very simple example, to help getting me going? 谁能给我一个非常简单的例子,以帮助我前进?

My code so far (without JSON) is something like this: 到目前为止,我的代码(没有JSON)是这样的:

try
  SourceFile.Position := 0;
  while TotalBytesRead < SourceFile.Size do
    begin 
      BytesRead := SourceFile.Read(Buffer,sizeof(Buffer));
      inc(TotalBytesRead, BytesRead);      
      StringContent := StripNonAsciiExceptCRLF(Buffer);    // A custom function to strip out binary garbage leaving just ASCII readable text     
      if Pos('MySearchValue', StringContent) > 0 then
          begin          
            // Do the parsing. This is where I need to do the JSON stuff

... ...

See parsedemo.pp , specially parsestring . 请参阅parsedemo.pp ,特别是parsestring Just modify the doparse routine to extract whatever information you need from the j:TJSonData object. 只需修改doparse例程即可从j:TJSonData对象提取所需的任何信息。

The simpledemo.pp demonstrates how to disect a jsondata object. simpledemo.pp演示了如何剖析jsondata对象。

Then read the src/README.txt file. 然后读取src / README.txt文件。 It seems to hint on a bunch of properties of a jsonobject that allow to get fields by name. 似乎暗示了jsonobject的一堆属性,这些属性允许按名称获取字段。 This only works for structures json objects like array and object. 这仅适用于结构json对象,例如array和object。

I do agree that a demo for that would be a good thing. 我同意这样做的演示是一件好事。 If you make it, please submit it to mantis. 如果成功,请提交给螳螂。

Disclaimer: I have nothing to do with the json package, I got the above from a quick glance at the (admittedly 2.7.1) source. 免责声明:我与json包无关,我快速浏览了(公认的2.7.1)源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM