J'essaie de trouver toutes les clés "a" dans une chaîne JSON et de les enregistrer dans un tableau. Comme ils peuvent être imbriqués à différents niveaux, j'utilise une récursive pour traverser les enfants de chaque clé pour répertorier de manière exhaustive toutes les clés "A". Cependant, je ne peux pas envelopper ma tête en recrutant les nœuds enfants et à ajouter des clés "A" trouvées à la matrice renvoyée. Toute aide ici serait appréciée.
La fonction pour traverser la chaîne JSON: p> Le résultat attendu est renvoyé pour contenir toutes les quatre clés "A" de l'objet JSON . P> Les données source: p>
3 Réponses :
Le moyen le plus simple d'interroger les sous-sections JSON est JONSPath Expressions , pour résoudre cette tâche Juste besoin d'appliquer 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"
}
}
]
}
};
Merci de la solution rapide, cependant, je voudrais accomplir ce qui précède sans introduire une nouvelle dépendance, si possible.
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 ) Voici un moyen possible d'utiliser Utilisez un array.pototype.flaTmap code> - 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))
// => []
Quel type d'application crée-tu - nœud.js ou Web?
c'est un nœud.js basé un.
S'il vous plaît lire exemple de reproductible minimal et mettez-le dans la question elle-même b> à la place.
Un objet JSON i> - JSON est toujours une chaîne.
@Quentin, merci d'avoir souligné. Prendra en compte à l'avenir.