从嵌套的JSON中提取特定值

时间:2019-12-28 01:36:28

标签: json delphi

如何从此JSON提取“用户名”值?

#include <iostream>
#include <sstream>
#include <fstream>

int main(){
   std::fstream file;
       file.open("sample.txt"); //Opening the file. "sample.txt" here is the path of the file.
   std::string data;
   //Uncomment below lines if you don't want to overwrite the files.
   /**
   std::stringstream filedata;
   filedata<<file.rdbuf();
   data.append(filedata.str());
   **/
   std::cin>>data;
   file<<data; //Appending to the file.
   return 0;
}

到目前为止我已经尝试了什么:

{"sections": [{"layout_type": "two_by_two_right", "layout_content": {"two_by_two_item": {"channel": {"title": "", "channel_id": "0_17843667847017167_2207568091687618311", "channel_type": "hashtag_videos", "header": "", "context": "", "media": {"taken_at": 1577382674, "pk": 2207568091687618311, "id": "2207568091687618311_3271302467", "device_timestamp": 1577382645873821, "media_type": 2, "code": "B6i23uyhwcH", "client_cache_key": "MjIwNzU2ODA5MTY4NzYxODMxMQ==.2", "filter_type": 0, "image_versions2": {"candidates": [{"width": 640, "height": 640, "url": "https://instagram.fmct3-1.fna.fbcdn.net/v/t51.2885-15/e35/75538180_158891128790372_2915252228956208913_n.jpg?_nc_ht=instagram.fmct3-1.fna.fbcdn.net\u0026_nc_cat=1\u0026_nc_ohc=hH1jCS_K4_0AX9C0T7i\u0026oh=4674fefc753d5bd0b8d73a1eae713ff9\u0026oe=5EB25F94\u0026ig_cache_key=MjIwNzU2ODA5MTY4NzYxODMxMQ%3D%3D.2"}, {"width": 480, "height": 480, "url": "https://instagram.fmct3-1.fna.fbcdn.net/v/t51.2885-15/e35/s480x480/75538180_158891128790372_2915252228956208913_n.jpg?_nc_ht=instagram.fmct3-1.fna.fbcdn.net\u0026_nc_cat=1\u0026_nc_ohc=hH1jCS_K4_0AX9C0T7i\u0026oh=d7d7c0dc6a85a7f84279f82f01ae648f\u0026oe=5E8F2B7C\u0026ig_cache_key=MjIwNzU2ODA5MTY4NzYxODMxMQ%3D%3D.2"}]}, "original_width": 640, "original_height": 640, "video_versions": [{"type": 101, "width": 480, "height": 480, "url": "https://instagram.fmct3-1.fna.fbcdn.net/v/t50.2886-16/81284582_860132527738160_2044993517273985872_n.mp4?_nc_ht=instagram.fmct3-1.fna.fbcdn.net\u0026_nc_cat=108\u0026_nc_ohc=prTN_4HS4ggAX-jMljp\u0026oe=5E08C64F\u0026oh=1f4f2ea83708e3cb237c734a13d77eb2", "id": "17869777126539044"}, {"type": 102, "width": 480, "height": 480, "url": "https://instagram.fmct3-1.fna.fbcdn.net/v/t50.2886-16/81284582_860132527738160_2044993517273985872_n.mp4?_nc_ht=instagram.fmct3-1.fna.fbcdn.net\u0026_nc_cat=108\u0026_nc_ohc=prTN_4HS4ggAX-jMljp\u0026oe=5E08C64F\u0026oh=1f4f2ea83708e3cb237c734a13d77eb2", "id": "17869777126539044"}, {"type": 103, "width": 480, "height": 480, "url": "https://instagram.fmct3-1.fna.fbcdn.net/v/t50.2886-16/81284582_860132527738160_2044993517273985872_n.mp4?_nc_ht=instagram.fmct3-1.fna.fbcdn.net\u0026_nc_cat=108\u0026_nc_ohc=prTN_4HS4ggAX-jMljp\u0026oe=5E08C64F\u0026oh=1f4f2ea83708e3cb237c734a13d77eb2", "id": "17869777126539044"}], "has_audio": true, "video_duration": 59.0, "view_count": 232325.0, "location": {"pk": 1548113472095913, "name": "Al Madina Restaurant \u0130stanbul \u0645\u0637\u0639\u0645 \u0627\u0644\u0645\u062f\u064a\u0646\u0629 \u0627\u0633\u0637\u0646\u0628\u0648\u0644", "address": "Katip Mustafa \u00c7elebi Mahallesi, \u0130stiklal Caddesi, No: 39, Beyo\u011flu", "city": "Taksim, Istanbul, Turkey", "short_name": "Al Madina Restaurant \u0130stanbul \u0645\u0637\u0639\u0645 \u0627\u0644\u0645\u062f\u064a\u0646\u0629 \u0627\u0633\u0637\u0646\u0628\u0648\u0644", "lng": 28.98347, "lat": 41.03591, "external_source": "facebook_places", "facebook_places_id": 1548113472095913}, "lat": 41.03591, "lng": 28.98347, "user": {"pk": 3271302467, "username": "almadinarestaurant", "full_name": "\u0645\u0637\u0639\u0645 \u0627\u0644\u0645\u062f\u064a\u0646\u0629 \u0627\u0633\u0637\u0646\u0628\u0648\u0644", "is_private": false, "profile_pic_url": "https://instagram.fmct3-1.fna.fbcdn.net/v/t51.2885-19/s150x150/39932201_486538485090431_6123883117892599808_n.jpg?_nc_ht=instagram.fmct3-1.fna.fbcdn.net\u0026_nc_ohc=wxoDrVKISKgAX_WWM4y\u0026oh=4a1673082b00303466847defe8f92b4e\u0026oe=5E9DA725", "profile_pic_id": "1852849531101432950_3271302467", "friendship_status": {"following": false, "outgoing_request": false, "is_bestie": false, "is_restricted": false}, "has_anonymous_profile_picture": false, "is_unpublished": false, "is_favorite": false, "can_see_primary_country_in_settings": false}, "can_viewer_reshare": true, "caption_is_edited": false, "comment_likes_enabled": false, "comment_threading_enabled": true, "has_more_comments": true, "next_max_id": 17876626159502158, "max_num_visible_preview_comments": 2, "preview_comments": [{"pk": 17915888494374591, "user_id": 6987374060, "text": "\u0647\u0627\u064a \u0627\u0644\u062f\u062c\u0627\u062c\u0647 \u0645\u0627\u0631\u0627\u064a\u062f\u0647 \u0639\u0645\u0631\u0647\u0627 \u0643\u0627\u0639\u062f \u062a\u0641\u062a\u0631 \u064a\u0645 \u0628\u0648\u0631\u0627\u0643", "type": 0, "created_at": 1577488612, "created_at_utc": 1577488612, "content_type": "comment", "status": "Active", "bit_flags": 0, "user": {"pk": 6987374060, "username": "noor_96raad", "full_name": "Noor Raad", "is_private": true, "profile_pic_url": "https://instagram.fmct3-1.fna.fbcdn.net/v/t51.2885-19/s150x150/26344158_141357603211756_640350538315071488_n.jpg?_nc_ht=instagram.fmct3-1.fna.fbcdn.net\u0026_nc_ohc=LFE2-wR4dagAX-XDuYU\u0026oh=0c72339e4a0c3b477e8fdf6449f4b915\u0026oe=5E98A360", "profile_pic_id": "1700100273510087891_6987374060", "is_verified": false}, "did_report_as_spam": false, "share_enabled": false, "media_id": 2207568091687618311, "has_translation": true}, {"pk": 17876626159502158, "user_id": 4515669677, "text": "\u0628\u062d\u0633 \u0627\u0646\u0643 \u0627\u0647\u0628\u0644 \u0648\u0627\u0646\u062a \u0628\u0627\u0635\u0635 \u0644\u0644\u0643\u0627\u0645\u064a\u0631\u0627 \u0643\u062f\u0627", "type": 0, "created_at": 1577492896, "created_at_utc": 1577492896, "content_type": "comment", "status": "Active", "bit_flags": 0, "user": {"pk": 4515669677, "username": "mohamedfarag855", "full_name": "Mohamed Farag", "is_private": false, "profile_pic_url": "https://instagram.fmct3-1.fna.fbcdn.net/v/t51.2885-19/s150x150/70126530_451622709033023_6583949114013122560_n.jpg?_nc_ht=instagram.fmct3-1.fna.fbcdn.net\u0026_nc_ohc=KvmYxtrSPO0AX-o0UK3\u0026oh=55a4efe0cc24bc6b9dc65dcc8e415334\u0026oe=5EA65D34", "profile_pic_id": "2148614249788813430_4515669677", "is_verified": false}, "did_report_as_spam": false, "share_enabled": false, "media_id": 2207568091687618311, "has_translation": true}], "can_view_more_preview_comments": true, "comment_count": 325, "like_count": 27989, "has_liked": false, "top_likers": [], "photo_of_you": false, "usertags": {"in": [{"user": {"pk": 303947497, "username": "cznburak", "full_name": "Burak \u00d6zdemir", "is_private": false, "profile_pic_url": "https://instagram.fmct3-1.fna.fbcdn.net/v/t51.2885-19/s150x150/74400352_1433463380151317_5481021398098903040_n.jpg?_nc_ht=instagram.fmct3-1.fna.fbcdn.net\u0026_nc_ohc=Op6_GQTK-XoAX9oe3dj\u0026oh=1227b143d2a639a4bf0266be36b5ae1f\u0026oe=5E9B58E0", "profile_pic_id": "2180993975116909614_303947497", "is_verified": true}, "position": [0.0, 0.0], 

我得到了AV。正确的方法是什么?我正在使用XSuperJSON最新版本。

更新

JSON太长,因此我将其粘贴到此pastebin JSON

1 个答案:

答案 0 :(得分:1)

您的代码没有深入到JSON层次结构中,无法到达'fill_items'数组和'username'字符串。

'sections'数组中,该数组中的所有对象都不具有'fill_items'子数组,因此在您的外部循环中,item.AsObject['fill_items']不返回有效元素。相反,这些对象有一个'layout_content'子对象,然后在该对象内部是'fill_items'数组或medias数组。

'fill_items'medias数组中,数组中的每个对象都有一个'media'子对象,该子对象有一个user子对象,而该子对象又有{{1 }}子字符串。

话虽如此,请尝试以下类似操作:

'username'

或者:

var
  json : ISuperObject;
  u : string;
  Item, Item2: IMember;
  LC: ISuperObject;
  Arr: ISuperExpression;
...
  json := TSuperObject.Create(list.Text);
  for item in json['sections'].AsArray do
  begin
    LC := item.AsObject['layout_content'].AsObject;
    Arr := LC['fill_items'];
    if Arr.DataType = dtNil then
      Arr := LC['medias'];
    for item2 in Arr.AsArray do
    begin
      u := item2.AsObject['media'].AsObject['user'].AsObject['username'].ToString;
    end;
  end;