代码之家  ›  专栏  ›  技术社区  ›  Shruti

如何在量角器中从嵌套的JSON结构中读取数据

  •  2
  • Shruti  · 技术社区  · 6 年前

    let web = JSON.parse(jsondata);
    let TestSuite = web["suite1"]["description"] 
    

    这是在控制台中提供数据,但是当我使用它时,

    let id = web["suite1"]["specs"]["id"] 
    

    它给出了未定义的。请帮帮我!

    {
    "suite1": {
        "id": "suite1",
        "description": "Login",
        "fullName": "Login",
        "failedExpectations": [],
        "status": "finished",
        "specs": [
            {
                "id": "spec0",
                "description": "Should able to login into the Distribution management Webpage",
                "fullName": "Login Should able to login into the Distribution management Webpage",
                "failedExpectations": [
                    {
                        "matcherName": "",
                        "message": "",
                        "stack": "",
                        "passed": false,
                        "expected": "",
                        "actual": ""
                    }
                ],
                "passedExpectations": [],
                "pendingReason": "",
                "started": "2018-09-06T06:57:42.740Z",
                "status": "failed",
                "duration": "7 secs",
                "stopped": "2018-09-06T06:57:49.255Z",
                "browserLogs": []
            }
        ]
    } }
    
    3 回复  |  直到 6 年前
        1
  •  0
  •   Faysal Ahmed    6 年前

    specs 包含对象数组。首先,需要从数组中获取对象,然后获取对象的值。

    试试这个:

    let id = web["suite1"]["specs"][0]["id"] 
    

    或者

    let id = web.suite1.specs[0].id
    

        2
  •  3
  •   Ankit    6 年前
    JSON.parse(JSON.stringify(json)).suite1.specs[0].id
    
        3
  •  0
  •   weirdpanda    6 年前

    当你这么做的时候 ["suite1"]["specs"] 0 -这个案子里的第三个。

    你可以通过键入 Object.prototype.toString.call( data["suite1"]["specs"]) .

    您可以尝试:

    data["suite1"]["specs"][0]["id"]
    

    data["suite1"]["specs"][0].id