【问题标题】:how to parse this json data in python如何在python中解析这个json数据
【发布时间】:2021-11-21 11:09:12
【问题描述】:

我有以下 JSON,例如:

{
  "name": "test",
  "version": "0.2.0",
  "lock": 1,
  "requires": true,
  "dependencies": {
    "@yamm/double": {
      "version": "7.14.5",
      "requires": {
        "@ginu/highlight": "^7.4.5"
      }
    },
    "@dauh/data": {
      "version": "7.15.0",
    },
    "@babel/core": {
      "version": "7.12.3",
      "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.12.3.tgz",
      "integrity": "sha512-0qXcZYKZp3/6N2jKYVxZv0aNCsxTSVCiK72DTiTYZAu7sjg73W0/aynWjMbiGd87EQL4WyA8reiJVh92AVla9g==",
      "requires": {
        "@babel/traverse": "^7.12.1",
        "@babel/types": "^7.12.1",
        "convert-source-map": "^1.7.0",
        "debug": "^4.1.0",
        "gensync": "^1.0.0-beta.1",
        "json5": "^2.1.2",
        "lodash": "^4.17.19",
        "resolve": "^1.3.2",
        "semver": "^5.4.1",
        "source-map": "^0.5.0"
      },

我只想用Python打印requires里面的数据

我正在尝试不同的方法,但它不起作用。请帮忙。我该怎么做?

【问题讨论】:

  • 应该是字符串,但不是……就是这样
  • 我只想打印里面需要使用 Python 的数据 仅供参考,该 JSON 文档中有 3x requires。另外,似乎已经是 json,您具体遇到了什么问题?在 Python 中访问字典?阅读docs
  • 有什么不同的方式给你试过了?您使用不同的方式遇到了哪些具体问题? “不工作”不足以描述您的问题。请使用tour、阅读what's on-topic hereHow to Askquestion checklist,并提供minimal reproducible example。欢迎使用 Stack Overflow!

标签: python json python-3.x parsing


【解决方案1】:

另一种方法:

data = {
  "name": "test",
  "version": "0.2.0",
  "lock": 1,
  "requires": 'true',
  "dependencies": {
    "@yamm/double": {
      "version": "7.14.5",
      "requires": {
        "@ginu/highlight": "^7.4.5"
      }
    },
    "@dauh/data": {
      "version": "7.15.0",
    },
    "@babel/core": {
      "version": "7.12.3",
      "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.12.3.tgz",
      "integrity": "sha512-0qXcZYKZp3/6N2jKYVxZv0aNCsxTSVCiK72DTiTYZAu7sjg73W0/aynWjMbiGd87EQL4WyA8reiJVh92AVla9g==",
      "requires": {
        "@babel/traverse": "^7.12.1",
        "@babel/types": "^7.12.1",
        "convert-source-map": "^1.7.0",
        "debug": "^4.1.0",
        "gensync": "^1.0.0-beta.1",
        "json5": "^2.1.2",
        "lodash": "^4.17.19",
        "resolve": "^1.3.2",
        "semver": "^5.4.1",
        "source-map": "^0.5.0"
      },
    }
}
}

# Actual code starts here
for module, dependencies in data['dependencies'].items():
    if requires := dependencies.get('requires'):
        print (f'Module {module} requires: ')
        for req, version in requires.items():
            print (f'\t{req}: {version}')

输出:

Module @yamm/double requires: 
    @ginu/highlight: ^7.4.5
Module @babel/core requires: 
    @babel/traverse: ^7.12.1
    @babel/types: ^7.12.1
    convert-source-map: ^1.7.0
    debug: ^4.1.0
    gensync: ^1.0.0-beta.1
    json5: ^2.1.2
    lodash: ^4.17.19
    resolve: ^1.3.2
    semver: ^5.4.1
    source-map: ^0.5.0

【讨论】:

    【解决方案2】:

    你可以使用python的json库来解析你的json。

    import json
    
    my_json = //your json statement
    
    parsed_json = my_json.loads()
    
    print(parsed_json["requires"])
    

    【讨论】:

    • 我以前试过。它会产生错误 - AttributeError: 'Response' object has no attribute 'loads'
    • .loads() 方法来自json,如json.loads(some_json_string)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 2016-12-07
    • 1970-01-01
    • 2022-12-02
    • 2020-08-27
    • 2022-01-16
    • 2020-08-10
    相关资源
    最近更新 更多