我一直試圖在一個包含地圖中的地圖的 JSON 中迭代幾個小時,但沒有運氣......
這是 JSON 字串:
{
"P31": {
"wikibase-entityid": "Q16603799"
},
"P227": {
"string": "1084653095"
},
"P1001": {
"wikibase-entityid": "Q183"
},
"P1448": {
"monolingualtext": "Verordnung über Sicherheit und Gesundheitsschutz bei der Verwendung von Arbeitsmitteln"
},
"P1813": {
"monolingualtext": "Betriebssicherheitsverordnung"
},
"P7677": {
"string": "betrsichv_2015"
},
"P580": {
"time": " 2002-10-03T00:00:00Z"
},
"P2671": {
"string": "/g/1224z0c0"
},
"P9696": {
"string": "11477"
}
}
影像便于視覺參考:

注意:每個屬性可以有多個值。
我基本上想創建一個回圈,在其中我可以訪問屬性(PXXX,第一件事)、屬性的型別(內部映射中的鍵)和屬性的值(內部映射中的值)。
我嘗試使用“new Map(JSON.parse(jsonStr))”、“new Map(Object.entries(jsonStr))”等將字串轉換為 Map,但沒有成功。
此外,我嘗試使用“for (var key in obj)”和“myMap.forEach((value_propertyInfo, key_propertyName) => {...}”在其中進行迭代,也沒有運氣。
有時看起來我正在逐個字符地迭代,而其他人只是拋出一個錯誤,說地圖不可迭代。
有人知道我應該改用什么嗎?
謝謝!
uj5u.com熱心網友回復:
不要打擾Map。只需遍歷資料物件,記錄鍵,然后記錄該屬性的物件條目,并記錄鍵和值。
const data = {"P31":{"wikibase-entityid":"Q16603799"},"P227":{"string":"1084653095"},"P1001":{"wikibase-entityid":"Q183"},"P1448":{"monolingualtext":"Verordnung über Sicherheit und Gesundheitsschutz bei der Verwendung von Arbeitsmitteln"},"P1813":{"monolingualtext":"Betriebssicherheitsverordnung"},"P7677":{"string":"betrsichv_2015"},"P580":{"time":" 2002-10-03T00:00:00Z"},"P2671":{"string":"/g/1224z0c0"},"P9696":{"string":"11477"}};
for (const key in data) {
console.log(key);
const entries = Object.entries(data[key]);
for (const [key, value] of entries) {
console.log(key, value);
}
}
uj5u.com熱心網友回復:
您可以遍歷物件屬性并提取每個專案的 propertyId、propertyName 和 propertyValue,如下所示:
let o = //the object coming from the json
Object.keys(o).forEach( (propertyId) => {
//I'm taking for granted that each item will contain one property only and that property will be what we are looking for
let propertyType = Object.keys( o[propertyId] )[0];
let propertyValue = o[propertyId][propertyType];
});
uj5u.com熱心網友回復:
資料結構確實不是最理想的。它使用變數屬性名稱、外部物件中的“PXXX”和內部物件中的型別。理想情況下,您始終擁有固定的屬性名稱,只有值會有所不同。最好的是這種形式的陣列和迭代
a=[
[prop, type, value],
[prop, type, value], // {"prop": prop, "type": type, "value":value} is also good
...
]
for(const [prop, type, value] of a) handleProperty(prop, type, value);
我們幾乎可以通過在決議之前轉換 json 字串來實作這一點:
s='{"P31":{"wikibase-entityid":"Q16603799"},"P227":{"string":"1084653095"},"P1001":{"wikibase-entityid":"Q183"},"P1448":{"monolingualtext":"Verordnung über Sicherheit und Gesundheitsschutz bei der Verwendung von Arbeitsmitteln"},"P1813":{"monolingualtext":"Betriebssicherheitsverordnung"},"P7677":{"string":"betrsichv_2015"},"P580":{"time":" 2002-10-03T00:00:00Z"},"P2671":{"string":"/g/1224z0c0"},"P9696":{"string":"11477"}}'
data=JSON.parse(s.replaceAll(':{',':[').replaceAll('":"','","').replaceAll('"}','"]'))
/* We now have
data={
prop:[type, value],
prop:[type, value],
...
}*/
for(const [prop,[type, value]] of Object.entries(data)) console.log(prop,type,value)
使用Object.entries()和解構賦值。
如果您不想轉換 JSON 字串,則必須使用
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/456000.html
標籤:javascript json 循环 字典 格式
下一篇:XQuery中字典中的for回圈
