0

I'm trying to parse JSON string (http://pastebin.com/zXn8pwtL):

sListing contains JSON string

jsObj: TJSONObject;
jsItem, jsElem, jsAsserts: TJSONValue;
...
jsObj := TJSONObject.ParseJSONValue(sListing) as TJSONObject;
jsAsserts := TJSONObject(TJSONObject(jsObj.Get('assets').JsonValue).Get('730').JsonValue).Get('2').JsonValue;

for jsElem in TJSONArray(jsAsserts) do
  WriteLn(jsElem.ToString);

How can I get all values from jsElem->descriptions? I've tried to enum all:

for jsItem in TJSONArray(jsElem) do
  WriteLn(jsItem.ToString);

But got EAccessViolation Read of address 00000001

Alex Hide
  • 555
  • 5
  • 18
  • Check that the casts are valid. Use `as` rather than unchecked casts. – David Heffernan Sep 08 '14 at 15:48
  • You are in the `assets/730/2` path where is no array. Array is if you go deeper to the specific object like e.g. `652761226` which contains `descriptions` array. – TLama Sep 08 '14 at 15:49
  • @DavidHeffernan Thanks, but if I use `as` compiler gives me an error: `Invalid class typecast` – Alex Hide Sep 08 '14 at 15:51
  • 1
    Yes, because there is no array. Here is [`your JSON simplified`](http://pastebin.com/Crb5THs2). – TLama Sep 08 '14 at 15:51
  • 6
    Well, the correct response to that is to accept what the compiler tells you! Rather than deciding that you know better and plugging on regardless. If a checked cast won't work, how could an unchecked cast? – David Heffernan Sep 08 '14 at 15:51
  • Are you sure that this is properly formed JSON? "results_html":"
    – alcalde Sep 08 '14 at 18:16
  • possible duplicate of [How to parse a JSON string in Delphi?](http://stackoverflow.com/questions/4350886/how-to-parse-a-json-string-in-delphi) – Sebastian Zartner Jan 15 '15 at 14:11

1 Answers1

1

Ok. Thanks for all. It was some misunderstanding with JSON types. It is fixed now:

jsAsserts: TJSONObject;
jsDesc: TJSONArray;
iCurEl: integer;
jsItem: TJSONPair;
...
jsAsserts := TJSONObject(TJSONObject(jsObj.Get('assets').JsonValue).Get('730').JsonValue).Get('2').JsonValue as TJSONObject;
for iCurEl := 0 to jsAsserts.Count - 1 do
  begin
    jsItem := jsAsserts.Pairs[iCurEl];
    jsDesc := (jsItem.JsonValue as TJSONObject).Get('descriptions').JsonValue as TJSONArray;
    WriteLn(jsDesc.ToString);
  end;
Alex Hide
  • 555
  • 5
  • 18