如何访问JSON对象数组的第一个元素? [英] How to access first element of JSON object array?
本文介绍了如何访问JSON对象数组的第一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我认为mandrill_events只包含一个对象。如何访问 event-property
?
I exptect that mandrill_events only contains one object. How do I access its event-property
?
var req = { mandrill_events: '[{"event":"inbound","ts":1426249238}]' }
推荐答案
要回答你的名义问题,你可以使用 [0]
来访问第一个元素,但是因为它是 mandrill_events
包含一个不是数组的字符串,所以 mandrill_events [0]
只会得到第一个字符'['。
To answer your titular question, you use [0]
to access the first element, but as it stands mandrill_events
contains a string not an array, so mandrill_events[0]
will just get you the first character, '['.
因此,要么将您的来源更正为:
So either correct your source to:
var req = { mandrill_events: [{"event":"inbound","ts":1426249238}] };
然后 req.mandrill_events [0]
,或者如果你坚持使用它是一个字符串,解析字符串包含的JSON:
and then req.mandrill_events[0]
, or if you're stuck with it being a string, parse the JSON the string contains:
var req = { mandrill_events: '[{"event":"inbound","ts":1426249238}]' };
var mandrill_events = JSON.parse(req.mandrill_events);
var result = mandrill_events[0];
这篇关于如何访问JSON对象数组的第一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文