json parse

yashinayashina Member Posts: 16
I have a json file
jo0ql9trdese.png
I want read total_pages

Name DataType Subtype Length
JToken DotNet Newtonsoft.Json.Linq.JToken.'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
ItemList DotNet Newtonsoft.Json.Linq.JArray.'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
Item DotNet Newtonsoft.Json.Linq.JObject.'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'

result := HttpResponseMessage.Content.ReadAsStringAsync.Result;
i :=0;
JToken := JToken.Parse(result);
ItemList := JToken.SelectToken('pagination');
FOREACH Item IN ItemList DO BEGIN
i :=i+1;
IF i=3 THEN BEGIN
MESSAGE(Item.GetValue('total_pages').ToString);
EVALUATE(TotalPage,Item.GetValue('total_pages').ToString);
END;
END;

But Error in Item.GetValue('total_pages').ToString
oiipdkmsnyhv.png
Whats wrong ?

Answers

  • Jan_VeenendaalJan_Veenendaal Member Posts: 206
    You have defined 'itemlist' to be a JArray, but in your json I don't see that it is an array - it looks like a nested object?
    And why do you loop through the elements? I think the memberlist of a json object is unordered, so you should not assume that the third element is the "total_pages" element.
    Jan Veenendaal
  • yashinayashina Member Posts: 16
    I understand that this is an object, not an array. How to correctly access an nested object and its value? For the first time I work with JSON.
  • yashinayashina Member Posts: 16
    thank you! You are very helpful. I understood what's the matter.
Sign In or Register to comment.