在 JS 中用映射迭代 JSON 字符串

Iterating a JSON string with maps in JS

我一直在尝试在一个 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 (obj 中的 var key)”和“myMap.forEach((value_propertyInfo, key_propertyName) => { ...}" 也没有运气。

有时看起来我正在一个字符一个字符地迭代,而其他人只是抛出一个错误,指出地图不可迭代。

有人知道我应该用什么代替吗?

谢谢!

您可以遍历对象属性并提取每个项目的 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];

});

不用理会 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);
  }
}

数据结构真的不是最理想的。它使用变量 属性 名称,外部对象为“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() and destructuring assignment.

如果您不想转换 JSON 字符串,则必须使用