【问题标题】:Adding separate tweet data from JSON onto a List<String>将来自 JSON 的单独推文数据添加到 List<String>
【发布时间】:2021-12-26 05:33:01
【问题描述】:

我直接从Twitter's API example using Postman 获取了以下 JSON 示例,我的问题是我如何能够抓取“数据”部分中的每条推文并将每条推文添加到 List 以便每个人推文的子 JSON 在列表中保存为字符串。这可能吗?我试图使用来自 Dart 的 JSON 解析方法解码和编码,但这不起作用,我不确定它是否因为 JSON 示例具有“数据”部分和“错误”部分。任何帮助将不胜感激,谢谢!

示例 JSON:

{
  "data": [
    {
      "author_id": "12",
      "conversation_id": "20",
      "created_at": "2006-03-21T20:50:14.000Z",
      "id": "20",
      "text": "just setting up my twttr"
    },
    {
      "attachments": {
        "media_keys": [
          "16_1276500934466703361"
        ]
      },
      "author_id": "783214",
      "conversation_id": "1275244210439028736",
      "created_at": "2020-06-23T01:48:07.000Z",
      "entities": {
        "urls": [
          {
            "start": 112,
            "end": 135,
            "url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
            "expanded_url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
            "display_url": "pic.twitter.com/dpI2lRmj9F"
          }
        ]
      },
      "id": "1275244210439028736",
      "text": "Need to follow what’s happening in real time? Change your timeline to show latest Tweets instead of top Tweets."
    },
    {
      "attachments": {
        "media_keys": [
          "3_1274087263073255425"
        ]
      },
      "author_id": "783214",
      "conversation_id": "1274087687469715457",
      "created_at": "2020-06-19T21:12:32.000Z",
      "entities": {
        "mentions": [
          {
            "start": 13,
            "end": 22,
            "username": "YoliZama"
          }
        ],
        "urls": [
          {
            "start": 23,
            "end": 46,
            "url": "https://twitter.com/Twitter/status/1274087695145332736/photo/1",
            "expanded_url": "https://twitter.com/Twitter/status/1274087695145332736/photo/1",
            "display_url": "pic.twitter.com/lcGDLzAJIn"
          }
        ]
      },
      "id": "1274087695145332736",
      "referenced_tweets": [
        {
          "type": "replied_to",
          "id": "1274087694105075714"
        }
      ],
      "text": "???? Oakland\n????️ @YoliZama"
    },
    {
      "attachments": {
        "media_keys": [
          "3_1274086977952833536"
        ]
      },
      "author_id": "783214",
      "conversation_id": "1274087687469715457",
      "created_at": "2020-06-19T21:12:32.000Z",
      "entities": {
        "mentions": [
          {
            "start": 19,
            "end": 31,
            "username": "Afrikkana95"
          }
        ],
        "urls": [
          {
            "start": 32,
            "end": 55,
            "url": "https://twitter.com/Twitter/status/1274087694105075714/photo/1",
            "expanded_url": "https://twitter.com/Twitter/status/1274087694105075714/photo/1",
            "display_url": "pic.twitter.com/tEfs27p7xu"
          }
        ]
      },
      "id": "1274087694105075714",
      "referenced_tweets": [
        {
          "type": "replied_to",
          "id": "1274087692003770368"
        }
      ],
      "text": "???? New York City\n????️ @Afrikkana95"
    },
    {
      "attachments": {
        "media_keys": [
          "3_1274086862907305984"
        ]
      },
      "author_id": "783214",
      "conversation_id": "1274087687469715457",
      "created_at": "2020-06-19T21:12:31.000Z",
      "entities": {
        "mentions": [
          {
            "start": 13,
            "end": 25,
            "username": "JoshuaKissi"
          }
        ],
        "urls": [
          {
            "start": 26,
            "end": 49,
            "url": "https://twitter.com/Twitter/status/1274087692003770368/photo/1",
            "expanded_url": "https://twitter.com/Twitter/status/1274087692003770368/photo/1",
            "display_url": "pic.twitter.com/ZeD3XvJUbX"
          }
        ]
      },
      "id": "1274087692003770368",
      "referenced_tweets": [
        {
          "type": "replied_to",
          "id": "1274087690758090752"
        }
      ],
      "text": "???? Chicago\n????️ @JoshuaKissi"
    },
    {
      "attachments": {
        "media_keys": [
          "3_1274086703272038401"
        ]
      },
      "author_id": "783214",
      "conversation_id": "1274087687469715457",
      "created_at": "2020-06-19T21:12:31.000Z",
      "entities": {
        "mentions": [
          {
            "start": 18,
            "end": 33,
            "username": "Imani_Barbarin"
          }
        ],
        "urls": [
          {
            "start": 34,
            "end": 57,
            "url": "https://twitter.com/Twitter/status/1274087690758090752/photo/1",
            "display_url": "pic.twitter.com/ZRDUipsu38",
            "expanded_url": "https://twitter.com/Twitter/status/1274087690758090752/photo/1",
            "display_url": "pic.twitter.com/ZRDUipsu38"
          }
        ]
      },
      "id": "1274087690758090752",
      "referenced_tweets": [
        {
          "type": "replied_to",
          "id": "1274087689487134720"
        }
      ],
      "text": "???? Philadelphia\n????️ @Imani_Barbarin "
    },
    {
      "attachments": {
        "media_keys": [
          "3_1274086530919718917"
        ]
      },
      "author_id": "783214",
      "conversation_id": "1274087687469715457",
      "created_at": "2020-06-19T21:12:30.000Z",
      "entities": {
        "mentions": [
          {
            "start": 13,
            "end": 25,
            "username": "BerniceKing"
          }
        ],
        "urls": [
          {
            "start": 26,
            "end": 49,
            "url": "https://twitter.com/Twitter/status/1274087688321200128/photo/1",
            "expanded_url": "https://twitter.com/Twitter/status/1274087688321200128/photo/1",
            "display_url": "pic.twitter.com/83upyVnwIS"
          }
        ]
      },
      "id": "1274087688321200128",
      "referenced_tweets": [
        {
          "type": "replied_to",
          "id": "1274087687469715457"
        }
      ],
      "text": "???? Atlanta\n????️ @BerniceKing "
    },
    {
      "attachments": {
        "media_keys": [
          "3_1274086027544498176"
        ]
      },
      "author_id": "783214",
      "conversation_id": "1274087687469715457",
      "created_at": "2020-06-19T21:12:30.000Z",
      "entities": {
        "mentions": [
          {
            "start": 17,
            "end": 29,
            "username": "FredTJoseph"
          }
        ],
        "urls": [
          {
            "start": 30,
            "end": 53,
            "url": "https://twitter.com/Twitter/status/1274087687469715457/photo/1",
            "expanded_url": "https://twitter.com/Twitter/status/1274087687469715457/photo/1",
            "display_url": "pic.twitter.com/lNTOkyguG1"
          }
        ]
      },
      "id": "1274087687469715457",
      "text": "???? Minneapolis\n????️ @FredTJoseph"
    },
    {
      "author_id": "783214",
      "conversation_id": "1274034244700930049",
      "created_at": "2020-06-19T17:40:09.000Z",
      "entities": {
        "hashtags": [
          {
            "start": 106,
            "end": 115,
            "tag": "BlackJoy"
          }
        ],
        "mentions": [
          {
            "start": 3,
            "end": 14,
            "username": "Blackbirds"
          }
        ]
      },
      "id": "1274034244700930049",
      "referenced_tweets": [
        {
          "type": "retweeted",
          "id": "1274014870707437570"
        }
      ],
      "text": "RT @Blackbirds: Juneteenth is a celebration. It’s about our freedom. And within that freedom is our joy.\n\n#BlackJoy is a form of resistance…"
    },
    {
      "author_id": "773578328498372608",
      "conversation_id": "1275473478779469825",
      "created_at": "2020-06-23T16:59:09.000Z",
      "entities": {
        "mentions": [
          {
            "start": 3,
            "end": 10,
            "username": "Policy"
          }
        ]
      },
      "id": "1275473478779469825",
      "referenced_tweets": [
        {
          "type": "retweeted",
          "id": "1275192966953476100"
        }
      ],
      "text": "RT @Policy: Statement on US high-skilled immigration proclamation: \n\n\"This proclamation undermines America’s greatest economic asset: its d…"
    }
  ],
  "errors": [
    {
      "detail": "Could not find tweet with ids: [1276230436478386177].",
      "title": "Not Found Error",
      "resource_type": "tweet",
      "parameter": "ids",
      "value": "1276230436478386177",
      "type": "https://api.twitter.com/2/problems/resource-not-found"
    }
  ]
}

输出:列表 列表中的一个元素可能如下所示:

{
  "attachments": {
    "media_keys": [
      "16_1276500934466703361"
    ]
  },
  "author_id": "783214",
  "conversation_id": "1275244210439028736",
  "created_at": "2020-06-23T01:48:07.000Z",
  "entities": {
    "urls": [
      {
        "start": 112,
        "end": 135,
        "url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
        "expanded_url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
        "display_url": "pic.twitter.com/dpI2lRmj9F"
      }
    ]
  },
  "id": "1275244210439028736",
  "text": "Need to follow what’s happening in real time? Change your timeline to show latest Tweets instead of top Tweets."
},

它基本上会抓取一整条推文的 subJson 并将其保存到列表中。这与我几天前遇到的关于 problem 的问题密切相关,但我认为问题是我使用 HTTP 库返回的 JSON 响应无法正确添加到列表中。

【问题讨论】:

  • 您能否添加一个示例,说明您希望结果数据结构如何来自您发布的示例输入?
  • @julemand101 我已经更新了帖子!

标签: json flutter dart


【解决方案1】:
List<Map<String, dynamic>> jsonRes =
    (inputMap['data'] as List<Map>).map((t) => {
        if (t.containsKey('attachments')) 'attachments': t['attachments'],
        if (t.containsKey('author_id')) 'author_id': t['author_id'],
        if (t.containsKey('conversation_id')) 'conversation_id': t['conversation_id'],
        if (t.containsKey('created_at')) 'created_at': t['created_at'],
        if (t.containsKey('entities')) 'entities': t['entities'],
        if (t.containsKey('id')) 'id': t['id'],
        if (t.containsKey('text')) 'text': t['text'],
    }).toList();

List<String> stringRes = jsonRes.map((e) => jsonEncode(e)).toList();

或者:

final List<String> wantedKeys = [
    'attachments',
    'author_id',
    'conversation_id',
    'created_at',
    'entities',
    'id',
    'text'
];
List<Map<String, dynamic>> jsonRes = (inputMap['data']
        as List<Map<String, dynamic>>)
    .map((m) => Map.fromEntries(m.entries.where((e) => wantedKeys.contains(e.key))))
    .toList();

List<String> stringRes = jsonRes.map((e) => jsonEncode(e)).toList();

【讨论】:

    猜你喜欢
    • 2012-12-26
    • 2011-08-03
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多