有什么方法可以解析这个JSON数组 - 我注意到有人为pascal脚本编写了json解析器-github.com/koldev/JsonParser,解析之后如何使用它来读取数组值,让我说我想在这个数组中找到“PrivacyPolicyUrl”的值?
{
"Offers": [
{
"DownloadUrl": "http://dehosting.dmccint.com/FeedStub/1.4.0.5.150107.02/stub.exe",
"OfferCMD": "OfferID=553565;;;DownloadUrl=http://files.brothersoft.com/yahoo-widgets/photos/Popup-Picture.widget;;;CMD=;;;SuccessCode=0;;;SessionId=07202d21-355a-4e52-affe-1cf255219ebc;;;PublisherID=123;;;GId=N.A;;;",
"SuccessCodes": "0",
"PrivacyPolicyUrl": null,
"TermsOfUseUrl": null,
"RegCheck": [],
"OfferID": 553565,
"OfferType": 1,
"Title": "Program1",
"Description": "A Program used for monitoring purpose",
"ImageUrl": "http://cmsstorage.dmccint.com/img/offers/bs_general_images.jpg",
"RevRate": 0
}
]
}
答案 0 :(得分:1)
它有效,10x
我已经改变了你的代码以适应我的需要,这是为了能够读取数组中的所有字段,我的代码可以正常工作,但我仍然坚持阅读整数(例如" OfferID&#34 ;值),我得到编译错误,请看 -
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
OutputDir=C:\Users\elramv\Desktop\json parser
[Code]
#include "JsonParser.pas"
procedure ProcessOutput(const Output: TJsonParserOutput);
var
S: string;
N: Integer;
I: Integer;
Offer: TJsonObject;
Offers: TJsonArray;
begin
// the JSON parser has the root as the first object, so let's ask if this object is named
// Offers and is of an array type; if yes, store the array to a local variable Offers, if
// not, raise an exception
Output.Objects[0][0].Key := 'Offers'
Offers := Output.Arrays[Output.Objects[0][0].Value.Index];
// having Offers array stored in the Offers variable, we can pick e.g. the first one ([0]),
// and check if that element is of type object; if yes, store it into the Offer variable,
// if not raise an exception
if (Offers[0].Kind = JVKObject) then
Offer := Output.Objects[Offers[0].Index]
else
RaiseException('Offers array element is not an object.');
// now we have the offer object, so let's try to find its PrivacyPolicyUrl value pair
for I := 0 to GetArrayLength(Offer) - 1 do
begin
// check its value type
case Offer[I].Value.Kind of
JVKWord:
begin
// check if the PrivacyPolicyUrl value contains the word 'null'; if so, output it, if
// not, raise an exception (in case it is unknown, 'true', or 'false')
if Output.Words[Offer[I].Value.Index] = JWNull then
S := 'null'
else
RaiseException('Unexpected PrivacyPolicyUrl value.');
end;
// the PrivacyPolicyUrl value contains a string value, so let's output it
JVKString: S := Output.Strings[Offer[I].Value.Index];
else
// otherwise, the PrivacyPolicyUrl value has an unexpected type
RaiseException('Unexpected PrivacyPolicyUrl value type.');
end;
// the PrivacyPolicyUrl value contains a string value, so let's output it
JVKNumber: S := Output.Numbers[Offer[I].Value.Index];
else
// otherwise, the PrivacyPolicyUrl value has an unexpected type
RaiseException('Unexpected PrivacyPolicyUrl value type.');
end;
MsgBox(Format('%s', [S]), mbInformation, MB_OK);
end;
end;