简体   繁体   English

Node.js:使用“ @”-符号解析json-ld / JSON

[英]Node.js: Parsing a json-ld / JSON with “@”-symbol

I have a JSON that looks like this: 我有一个看起来像这样的JSON:

{"marker":[{"@attributes":{"start":"Im Berge",
"finish":"Eichelberger Stra\u00dfe"
...

I am trying to parse the attributes inside the "@attributes", but have not found a way to do it. 我正在尝试解析“ @attributes”中的属性,但是还没有找到一种方法。 What I tried so far: 到目前为止我尝试过的是:

const fs = require('fs');  
var jsonObj = JSON.parse(fs.readFileSync('route1.json', 'utf8'));  
console.log(jsonObj['@attributes']);

Also tried the same with 也尝试过与

console.log(jsonObj.marker['@attributes']);

Neither of which work. 两者都不起作用。 I understand that this is supposed to be a json-ld and that I'm supposed to parse an object with an "@" sign with ['@attributes'], but either way I always get an error or undefined. 我知道这应该是json-ld,并且应该使用['@attributes']来解析带有“ @”符号的对象,但是无论哪种方式,我总是会得到错误或未定义。 I got the JSON from an API I wanna use and it is in there multiple times, so I have no way around it. 我从想要使用的API中获得了JSON,并且该API存在于其中多次,因此我无法解决。

.marker is an array so: .marker是一个array因此:

console.log(jsonObj.marker[0]['@attributes']);

But you may want to loop through it: 但是您可能想要遍历它:

jsonObj.marker.forEach(marker => console.log(marker['@attributes']));

You can require a JSON file, instead of JSON.parse & fs.readFileSync 您可以require使用JSON文件,而不是JSON.parsefs.readFileSync

var jsonObj = require('./route1.json');

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

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