【问题标题】:To extract specific key into a list from JSON从 JSON 中提取特定键到列表中
【发布时间】:2023-03-16 07:40:01
【问题描述】:

如何将 JSON 下面的 terms 的值以相反的顺序提取到列表中。在此示例中,我希望输出为 ["Auto Ontology","hdfc bank","giftplus card"]

{
  "faqs": [
    {
      "question": "Can the value of the HDFC Bank Giftplus card be topped up?",
      "alternateQuestions": [

      ],
      "terms": [
        "giftplus card",
        "hdfc bank",
        "Auto Ontology"
      ],
      "tags": [

      ],
      "refId": "7aea18a1-32c8-5af4-8d77-da31f9c7ff26",
      "responseType": "message",
      "answer": [
        {
          "text": "Gifting we believe is special to the occasion and hence you cannot reload this card. Every occasion is a new occasion and deserves a new card.",
          "type": "basic",
          "channel": "default"
        }
      ],
      "alternateAnswers": [

      ]
    },**strong text**

【问题讨论】:

  • 到目前为止您尝试了哪些方法,您遇到了哪些具体问题? SO 不是代码编写服务。
  • 打印(some_json['faqs'][0]['terms'][::-1])

标签: json python-3.x


【解决方案1】:
  {
  "faqs": [
  {
  "question": "Can the value of the HDFC Bank Giftplus card be topped up?",
  "alternateQuestions": [

  ],
  "terms": [
    "giftplus card",
    "hdfc bank",
    "Auto Ontology"
  ],
  "tags": [

  ],
  "refId": "7aea18a1-32c8-5af4-8d77-da31f9c7ff26",
  "responseType": "message",
  "answer": [
    {
      "text": "Gifting we believe is special to the occasion and hence you cannot reload this card. Every occasion is a new occasion and deserves a new card.",
      "type": "basic",
      "channel": "default"
    }
  ],
  "alternateAnswers": [

  ]
}
]
}

你可以使用下面给出的python代码来打印相反的顺序

import json
f = open('rev.json', 'r') 
a = json.load(f)
b = a["faqs"][0]["terms"][::-1]
print(b)

输出:

['Auto Ontology', 'hdfc bank', 'giftplus card']

【讨论】:

    【解决方案2】:

    你可以尝试使用sn-p

    Var = a["faqs"][0]["terms"][::-1]
    

    ::-1 用于反转输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 2023-01-24
      • 2019-10-20
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 2021-02-08
      相关资源
      最近更新 更多