简体   繁体   English

大 Json 在 cypress 中解析,jsonPath 用法

[英]Big Json parsing in cypress, jsonPath usage

I'm new to cypress and I face a problem when trying to work with json responses.我是柏树的新手,在尝试处理 json 响应时遇到问题。 I am getting json with units structure(tree structure) and I need to get all the names from it.我得到带有单位结构(树结构)的 json,我需要从中获取所有名称。

I tried to use jsonpath dependenc and parse json using jp.query, but it doesn't works:我尝试使用 jsonpath dependenc 并使用 jp.query 解析 json,但它不起作用:

 cy.request(*some request*).its('body').then((body) => {
                let units = jp.query(body, "$..[?(@.type=='PROJECT')].name");
           })

I'm getting obj needs to be an object Assertion error here我得到obj needs to be an object Assertion error here

How to fix this or maybe there are any other ways to parse json in cypress?如何解决这个问题,或者还有其他方法可以在柏树中解析 json?

JSON Sample: JSON 样品:

[
{
    "id": 2,
    "name": "Solar",
    "children": [
        {
            "id": 3,
            "name": "Earth",
            "children": [
                {
                    "id": 4,
                    "name": "Moon",
                    "type": "STREAM",
                    "active": true
                }
            ],
            "type": "PROJECT",
            "active": true
        },
        {
            "id": 5,
            "name": "Jupiter",
            "children": [
                {
                    "id": 6,
                    "name": "Io",
                    "type": "STREAM",
                    "active": true
                },
                {
                    "id": 7,
                    "name": "Ganymede",
                    "type": "STREAM",
                    "active": true
                }
            ]},
    ],
    "type": "PROGRAM",
    "active": true
},
{
    "id": 9,
    "name": "Centaurus",
    "children": [
        {
            "id": 10,
            "name": "Alpha Centauri A",
            "children": [
                {
                    "id": 818,
                    "name": "Alpha Centauri Aa-2345",
                    "type": "STREAM",
                    "active": true
                }
            ],
            "type": "PROJECT",
            "active": true
        },
        {
            "id": 11,
            "name": "Alpha Centauri B",
            "children": [
                {
                    "id": 12,
                    "name": "Alpha Centauri Bb",
                    "type": "STREAM",
                    "active": true
                }
            ],
            "type": "PROJECT",
            "active": true
        },
    ],
    "type": "PROGRAM",
    "active": true
}

] ]

add below 2 lines before jp.query()jp.query()之前添加以下两行

body = JSON.stringify(body);
body = JSON.parse(body);

Depending on how the request/response is configured, you may have one of the following issues根据请求/响应的配置方式,您可能会遇到以下问题之一

  • response is a string响应是一个字符串
  • body is a string body是一个字符串
  • there is no body没有body
cy.request(*some request*)
  .then(response => response.json())
  .its('body')
  .then(body => {
    let units = jp.query(body, "$..[?(@.type=='PROJECT')].name");
  })

or或者

cy.request(*some request*)
  .its('body')
  .then(body => body.json())
  .then(body => {
    let units = jp.query(body, "$..[?(@.type=='PROJECT')].name");
  })

or或者

cy.request(*some request*)
  .then(response => response.json())
  .then(jsonObj => {
    let units = jp.query(jsonObj, "$..[?(@.type=='PROJECT')].name");
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM