3 Réponses :


0
votes

Le moyen le plus simple d'interroger les sous-sections JSON est JONSPath Expressions , pour résoudre cette tâche Juste besoin d'appliquer $ .. A forte> -expression à sélectionner de manière récursive tous les descendants requis (voir ).

const dummyJSON = {
  "a": {
    "@attributes": {
      "href": "/URL/2021.html?q=link+value",
      "title": "Lautsprecher",
      "class": "catfilter-list-link",
      "data-gtm-event": "filter.click",
      "data-gtm-payload": "{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}",
      "data-metric-click": "mainsearch-catfilter-click"
    },
    "#text": "Lautsprecher"
  },
  "ul": {
    "@attributes": {
      "class": "catfilter-list"
    },
    "#text": [],
    "li": [
      {
        "@attributes": {
          "class": "catfilter-list-item"
        },
        "#text": [
          "",
          ""
        ],
        "a": {
          "@attributes": {
            "href": "/URL/8412.html?q=link+value",
            "title": "Handy-Ladegeräte",
            "class": "catfilter-list-link",
            "data-gtm-event": "filter.click",
            "data-gtm-payload": "{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}",
            "data-metric-click": "mainsearch-catfilter-click"
          },
          "#text": "Handy-Ladegeräte"
        }
      },
      {
        "@attributes": {
          "class": "catfilter-list-item"
        },
        "#text": [
          "",
          ""
        ],
        "a": {
          "@attributes": {
            "href": "/URL/26136.html?q=link+value",
            "title": "Kabellose Ladegeräte",
            "class": "catfilter-list-link",
            "data-gtm-event": "filter.click",
            "data-gtm-payload": "{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}",
            "data-metric-click": "mainsearch-catfilter-click"
          },
          "#text": "Kabellose Ladegeräte"
        }
      },
      {
        "@attributes": {
          "class": "catfilter-list-item"
        },
        "a": {
          "@attributes": {
            "href": "/URL/2021.html?q=link+value",
            "title": "Lautsprecher",
            "class": "catfilter-list-link",
            "data-gtm-event": "filter.click",
            "data-gtm-payload": "{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}",
            "data-metric-click": "mainsearch-catfilter-click"
          },
          "#text": "Lautsprecher"
        },
        "#text": [
          "",
          ""
        ],
        "a": {
          "@attributes": {
            "href": "/URL/2021.html?q=link+value",
            "title": "Lautsprecher",
            "class": "catfilter-list-link",
            "data-gtm-event": "filter.click",
            "data-gtm-payload": "{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}",
            "data-metric-click": "mainsearch-catfilter-click"
          },
          "#text": "Lautsprecher"
        }
      }
    ]
  }
};


1 commentaires

Merci de la solution rapide, cependant, je voudrais accomplir ce qui précède sans introduire une nouvelle dépendance, si possible.



1
votes

pour JSON String, le JSON.PARSE CODE> PARAMÈTRE REVIVIVER peut être utilisé:

p>

var arr = [], json = JSON.stringify({"a":{"@attributes":{"href":"/URL/2021.html?q=link+value","title":"Lautsprecher","class":"catfilter-list-link","data-gtm-event":"filter.click","data-gtm-payload":"{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}","data-metric-click":"mainsearch-catfilter-click"},"#text":"Lautsprecher"},"ul":{"@attributes":{"class":"catfilter-list"},"#text":[],"li":[{"@attributes":{"class":"catfilter-list-item"},"#text":["",""],"a":{"@attributes":{"href":"/URL/8412.html?q=link+value","title":"Handy-Ladegeräte","class":"catfilter-list-link","data-gtm-event":"filter.click","data-gtm-payload":"{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}","data-metric-click":"mainsearch-catfilter-click"},"#text":"Handy-Ladegeräte"}},{"@attributes":{"class":"catfilter-list-item"},"#text":["",""],"a":{"@attributes":{"href":"/URL/26136.html?q=link+value","title":"Kabellose Ladegeräte","class":"catfilter-list-link","data-gtm-event":"filter.click","data-gtm-payload":"{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}","data-metric-click":"mainsearch-catfilter-click"},"#text":"Kabellose Ladegeräte"}},{"@attributes":{"class":"catfilter-list-item"},"a":{"@attributes":{"href":"/URL/2021.html?q=link+value","title":"Lautsprecher","class":"catfilter-list-link","data-gtm-event":"filter.click","data-gtm-payload":"{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}","data-metric-click":"mainsearch-catfilter-click"},"#text":"Lautsprecher"},"#text":["",""],"a":{"@attributes":{"href":"/URL/2021.html?q=link+value","title":"Lautsprecher","class":"catfilter-list-link","data-gtm-event":"filter.click","data-gtm-payload":"{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}","data-metric-click":"mainsearch-catfilter-click"},"#text":"Lautsprecher"}}]}})

JSON.parse(json, (key, val) => key == 'a' ? arr.push(val) : val)

console.log( arr )


0 commentaires

1
votes

Voici un moyen possible d'utiliser array.pototype.flaTmap code> - xxx pré>

Utilisez un simplifié code> et testez la sortie -

const data =
  { a: { b: 0, c: { a: 1 } }
  , b: [ { a: 2 } ]
  , c: { d: 0 }
  , e: { f: { g: { h: { a: 3 } } } }
  , i: { j: [ { a: 4 }, { a: 5 } ] }
  }

const valuesForProp = (q = "", o = {}) =>
  Object (o) === o
    ? Object
        .entries (o)
        .flatMap
          ( ([ k, v ]) =>
              k === q
                ? [ v, ...valuesForProp (q, v) ]
                : valuesForProp (q, v)
          )
    : []

console .log (valuesForProp ("a", data))
// => [ { b: 0, c: { a: 1 } }, 1, 2, 3, 4, 5 ]

console .log (valuesForProp ("b", data))
// => [ 0, [ { a: 2 } ] ]

console .log (valuesForProp ("c", data))
// => [ { a: 1 }, { d: 0 } ]

console .log (valuesForProp ("z", data))
// => []


0 commentaires