在 JSON 数组中查找对象
Find Object in JSON Array
我一辈子都找不到 return JSON 数组中某个元素的正确语法
数组是
{
"_total": 1,
"values": [{
"isCommentable": true,
"isLikable": true,
"isLiked": false,
"numLikes": 0,
"timestamp": 1453718959851,
"updateComments": {"_total": 0},
"updateContent": {
"company": {
"id": 2691316,
"name": "Rising 5th Web Design"
},
"companyStatusUpdate": {"share": {
"comment": "This is a test update for testing the jQuery REST API",
"id": "s6097339248095035392",
"source": {
"serviceProvider": {"name": "LINKEDIN"},
"serviceProviderShareId": "s6097339248095035392"
},
"timestamp": 1453718959851,
"visibility": {"code": "anyone"}
}}
},
"updateKey": "UPDATE-c2691316-6097339248082460672",
"updateType": "CMPY"
}]
}
我要获取的元素是
"comment": "This is a test update for testing the jQuery REST API"
但没有充分的理由,我无法弄清楚如何将 PHP 遍历到此元素的语法。
如有任何帮助,我们将不胜感激。
尝试将您的 json 放入变量中,然后使用 json_decode() 解码 php 中的 json。
//json variable.
$json ='{
"_total": 1,
"values": [{
"isCommentable": true,
"isLikable": true,
"isLiked": false,
"numLikes": 0,
"timestamp": 1453718959851,
"updateComments": {"_total": 0},
"updateContent": {
"company": {
"id": 2691316,
"name": "Rising 5th Web Design"
},
"companyStatusUpdate": {"share": {
"comment": "This is a test update for testing the jQuery REST API",
"id": "s6097339248095035392",
"source": {
"serviceProvider": {"name": "LINKEDIN"},
"serviceProviderShareId": "s6097339248095035392"
},
"timestamp": 1453718959851,
"visibility": {"code": "anyone"}
}}
},
"updateKey": "UPDATE-c2691316-6097339248082460672",
"updateType": "CMPY"
}]
}';
// decode json
$c = json_decode($json, true);
// get your element
echo $c['values'][0]['updateContent']['companyStatusUpdate']['share']['comment'];
如果它是静态的 json,那么您可以使用 json_decode
:
$content = json_decode($string, true);
echo $content['values'][0]['updateContent']['companyStatusUpdate']['share']['comment'];
解释:
首先解码你的 json
并获得正确的索引。
请注意,在此示例中,我使用 json_decode()
函数的第二个参数作为 true
如果您忽略此参数,那么您将获得 OBJECT
形式的解码结果。
我一辈子都找不到 return JSON 数组中某个元素的正确语法
数组是
{
"_total": 1,
"values": [{
"isCommentable": true,
"isLikable": true,
"isLiked": false,
"numLikes": 0,
"timestamp": 1453718959851,
"updateComments": {"_total": 0},
"updateContent": {
"company": {
"id": 2691316,
"name": "Rising 5th Web Design"
},
"companyStatusUpdate": {"share": {
"comment": "This is a test update for testing the jQuery REST API",
"id": "s6097339248095035392",
"source": {
"serviceProvider": {"name": "LINKEDIN"},
"serviceProviderShareId": "s6097339248095035392"
},
"timestamp": 1453718959851,
"visibility": {"code": "anyone"}
}}
},
"updateKey": "UPDATE-c2691316-6097339248082460672",
"updateType": "CMPY"
}]
}
我要获取的元素是
"comment": "This is a test update for testing the jQuery REST API"
但没有充分的理由,我无法弄清楚如何将 PHP 遍历到此元素的语法。
如有任何帮助,我们将不胜感激。
尝试将您的 json 放入变量中,然后使用 json_decode() 解码 php 中的 json。
//json variable.
$json ='{
"_total": 1,
"values": [{
"isCommentable": true,
"isLikable": true,
"isLiked": false,
"numLikes": 0,
"timestamp": 1453718959851,
"updateComments": {"_total": 0},
"updateContent": {
"company": {
"id": 2691316,
"name": "Rising 5th Web Design"
},
"companyStatusUpdate": {"share": {
"comment": "This is a test update for testing the jQuery REST API",
"id": "s6097339248095035392",
"source": {
"serviceProvider": {"name": "LINKEDIN"},
"serviceProviderShareId": "s6097339248095035392"
},
"timestamp": 1453718959851,
"visibility": {"code": "anyone"}
}}
},
"updateKey": "UPDATE-c2691316-6097339248082460672",
"updateType": "CMPY"
}]
}';
// decode json
$c = json_decode($json, true);
// get your element
echo $c['values'][0]['updateContent']['companyStatusUpdate']['share']['comment'];
如果它是静态的 json,那么您可以使用 json_decode
:
$content = json_decode($string, true);
echo $content['values'][0]['updateContent']['companyStatusUpdate']['share']['comment'];
解释:
首先解码你的 json
并获得正确的索引。
请注意,在此示例中,我使用 json_decode()
函数的第二个参数作为 true
如果您忽略此参数,那么您将获得 OBJECT
形式的解码结果。