我正在尝试从JSON对象中获取数据,但是效果不佳。
这是我制作JSON对象(我在此项目中使用Twig
)的方法,后来尝试达到它以从中获取值:
<script>
let data = [
{% for entry in markers %}
{"name": "{{ entry.title }}", "address": "{{ entry.address.address }}", "lat": "{{ entry.address.lat }}", "lng": "{{ entry.address.lng }}"},
{% endfor %}
];
let json = JSON.parse(data); //it already goes wrong here.
alert(json.name);
</script>
这是我的对象的外观:
(6) [{…}, {…}, {…}, {…}, {…}, {…}]
0:
address: "Tilburg, Nederland"
lat: "51.56059600"
lng: "5.09191430"
name: "Tilburg"
__proto__: Object
1:
address: "Amsterdam, Nederland"
lat: "52.36798430"
lng: "4.90356140"
name: "Amsterdam"
__proto__: Object
2:
address: "Eindhoven, Nederland"
lat: "51.44164200"
lng: "5.46972250"
name: "eindhoven"
__proto__: Object
3:
address: "Drenthe, Nederland"
lat: "52.94760120"
lng: "6.62305860"
name: "drenthe"
__proto__: Object
4:
address: "Limburg, Nederland"
lat: "51.44272380"
lng: "6.06087260"
name: "limburg"
__proto__: Object
5:
address: "Arnhem, Nederland"
lat: "51.98510340"
lng: "5.89872960"
name: "test"
__proto__: Object
length: 6
__proto__: Array(0)
我认为我的错误在于制作物体的部分。但是我不知道我做错了什么。
编辑: 我忘记添加错误消息。这就是控制台在chrome中告诉我的内容:
VM9133:1未捕获的SyntaxError:JSON中的意外令牌o在位置 1个 在JSON.parse() at maps:63(anonymous)@ maps:63