简体   繁体   English

打印数组中的JSON值,并且此数组在另一个数组中

[英]Print value of JSON that is inside an array, and this array is inside another array

Maybe this problem is duplicated, but I don't find an answer that respond my problem. 也许这个问题是重复的,但我找不到能回答我问题的答案。

I am trying to access to a value in JSON, but this is in array, and that is inside another array. 我正在尝试访问JSON中的值,但这在数组中,并且在另一个数组中。 This is the example: 这是示例:

Original JSON: 原始JSON:

{"object":"page","entry":
[{"id":"488064221540488","time":1501681094769,"messaging":[{"sender":
{"id":"1559711167437676"},"recipient":
{"id":"488064221540488"},"timestamp":1501681094579,"message":
{"mid":"mid.$cAAHhEUCp_UVj1HBXs1do8-
BMiIPg","seq":559,"text":"Thunder."}}]}]}

I am trying to access to value of sender key: 我正在尝试访问发送方密钥的值:

...[{"sender": {"id: "1559711167437676"}, ...

I am trying access via properties of the original request, but I get undefined. 我正在尝试通过原始请求的属性进行访问,但未定义。 I am trying like this. 我正在尝试这样。

console.log(JSON.stringify(req.body.entry.messaging))    // At this point, I get undefined

I've tried with JSON.parser but the same result is thrown. 我已经尝试过使用JSON.parser,但是抛出了相同的结果。 This is only one of multiple statement that I've used to get the result. 这只是我用来获取结果的多条语句之一。

Someone can help me to print this exact value, you can see that is inside of array that is inside of another array, this sound complex, but I don't know. 有人可以帮助我打印此确切值,您可以看到它在另一个数组内部的数组中,这听起来很复杂,但我不知道。 Hope you can help me. 希望您能够帮助我。

entry is an array (take note of the square brackets), so you need to at least reference the index you are interested in. The same is true for messageing , so get to the id like this: entry是一个数组(请注意方括号),因此您至少需要引用您感兴趣的索引messageing也是如此,因此请获取如下id

req.body.entry[0].messaging[0].sender.id

 const req = { body: { "object": "page", "entry": [{ "id": "488064221540488", "time": 1501681094769, "messaging": [{ "sender": { "id": "1559711167437676" }, "recipient": { "id": "488064221540488" }, "timestamp": 1501681094579, "message": { "mid": "mid.$cAAHhEUCp_UVj1HBXs1do8-BMiIPg", "seq": 559, "text": "Thunder." } }] }] } }; const id = req.body.entry[0].messaging[0].sender.id; console.log(id, parseInt(id)); 

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

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