0

Is there any way I can parse this JSON array - I've noticed someone wrote JSON parser for pascal script here -https://github.com/koldev/JsonParser. Also after parsing, how do I use it to read an array value? Let's say I want to find the value for PrivacyPolicyUrl in this array?

{
    "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
        }
    ]
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
ElramV
  • 325
  • 4
  • 16

1 Answers1

1

Example of processing JSON arrays:

[Setup]
#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;  
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
ElramV
  • 325
  • 4
  • 16
  • 1
    Your `case` switch has too many `else`s. Write [`something like this`](http://pastebin.com/sc08nG3i). But looking at your modifications it seems that you are not sure with that `PrivacyPolicyUrl` value type. I just add, that if it's an object, you will need to get it from the `Objects` collection. – TLama Jan 20 '15 at 18:00