Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON using lkJSON

I have a JSON file which I need to parse and extract one value.

{
  "user": {
    "pk": 25025320,
    "username": "instagram",
    "full_name": "Instagram",
    "is_private": false,
    "profile_pic_url": "https://instagram.fmad3-5.fna.fbcdn.net/vp/295f9e76d3c26fdf613d7856e7b43348/5B4F495B/t51.2885-19/s150x150/14719833_310540259320655_1605122788543168512_a.jpg",
    "profile_pic_id": "1360316971354486387_25025320",
    "is_verified": true,
    "has_anonymous_profile_picture": false,
    "media_count": 5164,
    "follower_count": 233901016,
    "following_count": 187,
    "biography": "Discovering — and telling — stories from around the world.",
    "external_url": "",
    "usertags_count": 144,
    "hd_profile_pic_versions": [
      {
        "width": 320,
        "height": 320,
        "url": "https://instagram.fmad3-5.fna.fbcdn.net/vp/893534d61bdc5ea6911593d3ee0a1922/5B6363AB/t51.2885-19/s320x320/14719833_310540259320655_1605122788543168512_a.jpg"
      }
    ],
    "hd_profile_pic_url_info": {
      "url": "https://instagram.fmad3-5.fna.fbcdn.net/vp/8ae8a89c80ff4722eeab592b685276cb/5B5D40A1/t51.2885-19/14719833_310540259320655_1605122788543168512_a.jpg",
      "width": 320,
      "height": 320
    },
    "has_highlight_reels": true,
    "auto_expand_chaining": false
  },
  "status": "ok"
}

I want to extract and send the value of hd_profile_pic_url_info.url to Memo2 using the lkJSON library, Instagram API and code below to achievie this result.

var
  sJSON: string;
  js, Items, Item: TlkJSONbase;
  I, J: Integer;
begin
  sJSON := Memo1.text;
  js := TlkJSON.ParseText(sJSON);
  for I := 0 to Pred(js.Count) do
  begin
    Items := js.Child[I].Field['user'];
    for J := 0 to Pred(Items.Count) do
    begin
      Item := Items.Child[J];
      Memo2.Lines.Add(VarToStr(Item.Field['hd_profile_pic_url_info'].Value));
    end;
  end;
end;
like image 828
peterva Avatar asked Jun 21 '26 08:06

peterva


2 Answers

You forgot that hd_profile_pic_url_info is another object containing url, width, and height, so you need to go down one more time. Here is an example of how this should be done properly.

var
  js : TlkJSONbase;
begin
  js := TlkJSON.ParseText(Memo1.Text);
  if Assigned(js) then
  begin
    js := js.Field['user'];
    if Assigned(js) then
    begin
      js := js.Field['hd_profile_pic_url_info'];
      if Assigned(js) then
      begin
        js := js.Field['url'];
        if Assigned(js) then
           Memo2.Lines.Add(VarToStr(js.Value));
      end;
    end;
  end;
end;
like image 126
Triber Avatar answered Jun 23 '26 21:06

Triber


i use this function :

function ChildValueStr(AChild: TlkJSONbase; const Name: string; DefaultValue:
    AnsiString = ''): {$IF CompilerVersion>=26}WideString{$ELSE}AnsiString{$IFEND};
var
  tmpIndex: integer;
  tmpStr: {$IF CompilerVersion>=26}WideString{$ELSE}AnsiString{$IFEND};
begin
  Result:= DefaultValue;
  if IsNil(AChild) then exit;

  tmpStr:= (AChild as TlkJSONobject).{$IF CompilerVersion>=26}getWideString{$ELSE}getString{$IFEND}(Name);

  if tmpStr = '' then
  begin
    tmpIndex := (AChild as TlkJSONobject).IndexOfName(Name);
    if tmpIndex <> -1 then
      Result := (AChild as TlkJSONobject).getString(tmpIndex);
  end
  else Result:= tmpStr;
end;

so with your code, you can use it :

var sJSON: String;    
js, Items, Item: TlkJSONBase;    
I, J: Integer;    
begin    
    sJSON := Memo1.text;    
    js := TlkJSON.ParseText(sJSON);    
    for I := 0 to Pred(js.Count) do
    begin
        Items := js.Child[I].Field['user'];          
        Memo2.Lines.Add(ChildValueStr(Items, 'hd_profile_pic_url_info'));
    end;   
end;

I hope this can help u

like image 26
MSB Avatar answered Jun 23 '26 22:06

MSB