Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use superobject to parse Json data in Delphi7

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how Delphi7 uses superobject to analyze Json data. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Preface

Now, no matter Webapi or general data communication, we basically use Json. In the past, many applications are developed with delphi7. In order to maintain the old procedures and interface with the new ones (such as Wechat Alipay, etc.), we use superobject to manipulate Json data.

Since we are using D7, there is no usage such as generics, so there is no serialization and deserialization of classes that directly generate Json. It may be more troublesome to operate than Clover, Android, and so on.

Superobject unit

This can be downloaded from the Internet. Later, we will put this unit on my network disk. What we use here is

Is the above superobject.pas, my version is 1.25, originally used a 1.24 inside there are several small BUG (such as Currency type input 0 can not parse), solved in 1.25.

Basic usage

Var

J, a: ISuperObject

/ / cannot be TSuperObject, although there is a TSuperObject class.

Begin

J:=TSuperObject.Create (stObject)

/ / create a json object. If the parameter is stArray,j, it is a json array, and the parameter can also be other variable types, such as strings, numeric values, etc., which looks powerful, but if the parameter is a string that describes json, you will get an error. If you want to load from text, use j:=SO (json text)

JI ['number']: = 123

/ / is equal to {"number": 123}. Similarly, I can also be changed into something like SMagneBMagneC to represent string, Boolean, and currency.

J.S ['string']: = "value"

/ / is equal to {"string": "value"}, similarly, I can also be changed to SMagneBMagazine C and so on, to represent string, Boolean, currency.

A:=TSuperObject.Create (stArray)

/ / create a json array a

A.I [']: = 111

/ / means to add a 111 element to array a. Note that the value in [] is'', and the semantics of its [] have changed subversely in the array.

A.I [2]: = "s"

/ / the third element has a value of "s". Well, [] is used a lot in superobject.

J ['arr']: = a

/ / add the array a to j, whose key name is arr

Showmessage (j.toJson ())

/ / View the json text of j

J:=nil

/ / cannot write j.Free

End

Code demonstration

Because this is written while doing this in the project, let's take a look at our directory structure by using the "Delphi calling dynamic link library" PosPayIntf dynamic library project in the previous chapter.

We put all the Json processing in the Json folder under BaseClass.

Where superobject is the Json parsing unit that we refer to, and DoJson is a few of the methods I encapsulated to deal with the return.

We can see that these two unit files have been referenced in the dynamic library project.

DoJson unit

I wrote three functions in this unit.

GetJsonStr; gets the string under the project corresponding to Json

CreateOutMsgJsonStr; generates the Json string of the output OutMsg

CreateOutParaJsonStr; generates a string that outputs OutParaJsonStr

GetJsonStr

According to the input Json string, we find the specified attribute to get the corresponding string

As follows

The string of the Json passed in above

We get the corresponding value by using the following method

The first GetJsonStr (Inparastr,'payinfo'). After calling this, the string obtained by our tmpParaStr becomes {"oldsaleno": "," payamt ": 387.6," oldposno ":", "oldpayno": "," salen ".

O ":" 201708181158150001 "," oldsaledate ":", "oldrefinfo": ""}

The second GetJsonStr (tmppParaStr,'Payamt'), after calling this, our PayTotal value gets 387.6, and then we convert the string to Currency type.

OutParaJsonStr

Interface document

Generating function

Is a function that generates a Json string

I have added default values for the last few parameters, so if you don't have to type these parameters, you will directly give them default values when calling.

Call method

Returned Json

CreateOutMsgJsonStr

This method is basically the same as the above one, except for different parameters.

Interface document

application program interface

Call method

Returned Json

The display effect after the program call

This is how the Delphi7 shared by the editor parses Json data with superobject. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report