JsonPath

Ukázka práce s JSON Path

Popis

declare function JsonGetBoolean dll "ScriptEx" (string, string): boolean
declare function JsonGetCurrency dll "ScriptEx" (string, string): currency
declare function JsonGetInteger dll "ScriptEx" (string, string): integer
declare function JsonGetString dll "ScriptEx" (string, string): string

declare function JsonIsNull dll "ScriptEx" (string, string): boolean

declare function JsonGetArrayLength dll "ScriptEx" (string, string): integer

script JsonPathExample(): boolean
var
  Json: string
  I: integer
begin
  Json := "{"
  Json += "  ""people"": ["
  Json += "    {"
  Json += "      ""name"" : ""John"", "
  Json += "      ""surname"" : ""Doe"", "
  Json += "      ""phone"" : {"
  Json += "        ""number"": ""123 456 789"", "
  Json += "        ""is_cellphone"": true "
  Json += "      }, "
  Json += "      ""salary"" : ""4680,50"", "
  Json += "      ""age"" : 36, "
  Json += "      ""job"" : ""Police officer"", "
  Json += "      ""children"" : ["
  Json += "        {"
  Json += "          ""name"":""Mary"" "
  Json += "        },"
  Json += "        {"
  Json += "          ""name"":""Bob"" "
  Json += "        }"
  Json += "      ] "
  Json += "    },"
  Json += "    {"
  Json += "      ""name"" : ""Jane"", "
  Json += "      ""surname"" : ""Doe"", "
  Json += "      ""phone"" : {"
  Json += "        ""number"": ""987 654 321"", "
  Json += "        ""is_cellphone"": false "
  Json += "      }, "
  Json += "      ""salary"" : 0, "
  Json += "      ""age"" : 32, "
  Json += "      ""job"" : null, "
  Json += "      ""children"" : ["
  Json += "        {"
  Json += "          ""name"":""Mary"" "
  Json += "        },"
  Json += "        {"
  Json += "          ""name"":""Bob"" "
  Json += "        }"
  Json += "      ], "
  Json += "      ""complex.named key"" : ""complex named value"" "
  Json += "    }"
  Json += "  ]"
  Json += "}"

  // First person
  if JsonGetBoolean(Json, "people[0].phone.is_cellphone") then
    write("Cellphone number: " + JsonGetString(Json, "people[0].phone.number"))
  else
    write("Phone number: " + JsonGetString(Json, "people[0].phone.number"))
  end

  write("Salary: " + JsonGetCurrency(Json, "people[0].salary"))
  write("Age: " + JsonGetInteger(Json, "people[0].age"))
  write("Name: " + JsonGetString(Json, "people[0].name"))
  write("Surname: " + JsonGetString(Json, "people[0].surname"))

  if JsonIsNull(Json, "people[0].job") then
    write("Job: unemployed")
  else
    write("Job: " + JsonGetString(Json, "people[0].job"))
  end

  // Second person
  for I := 0 to JsonGetArrayLength(Json, "people[1].children") - 1 do
    write("" + (I + 1) + ". child name: " + JsonGetString(Json, "people[1].children[" + I + "].name"))
  end

  write("Complex: " + JsonGetString(Json, "people[1][""complex.named key""]"))

  result := true
end

Ukázky JSON Path

Cesta Popis
people[0].phone.number Telefoní číslo první osobny
people[1].children[1].name Jméno druhého dítětě druhé osoby
people[1]["complex.named key"] Ukázka přístupu k vlastnosti obsahjící "problematické" znaky