简体   繁体   English

从Json数组检索值

[英]Retrieve value from a Json Array

I'm having difficulty in retrieving values from a Json array. 我很难从Json数组中检索值。 I have a Json dataset that has been created using json_encode. 我有一个使用json_encode创建的Json数据集。 This is how it appears after using json_decode and displaying using print_r: 这是使用json_decode并使用print_r显示后的样子:

Array ( [0] => stdClass Object ( [postID] => 1961 [postTitle] => Kiss My Fairy [previewThumb] => 2011/09/Kiss-My-Fairy-Ibiza-essentialibiza-2011_feature.jpg [blogContent] => Ibiza has always had a flair for the extravagant, inhibitions are checked in at the airport when the floods of tourists arrive and the locals have embraced a freedom of partying that has turned the quiet Mediterranean island into the Mecca... ) ) a_post_data_array = 1

The code that achieves this is as follows: 实现此目的的代码如下:

$post_data_URL = "http://myurl.com/news/scrape/facebook-like-chart-ID.php?post_name=kiss-my-fairy";

$post_data_response = file_get_contents($post_data_URL);  

$a_post_data_array=json_decode($post_data_response);  

I now simply need to retrieve some of the values from this array to use as variables. 现在,我只需要从此数组中检索一些值即可用作变量。 The array will only ever have 1 set of values. 该数组将只有一组值。 I'm using the following code but it isn't working. 我正在使用以下代码,但无法正常工作。

echo "**** -- " . $a_post_data_array[post_title] . "<br>";

Can anyone please help? 谁能帮忙吗? I'm sorry this is so basic. 抱歉,这太基本了。 I've been searching online but can't find any examples of this. 我一直在网上搜索,但找不到任何示例。

echo "** -- " . $a_post_data_array[0]['post_title'];

There are multiple issues with your code: 您的代码存在多个问题:

  • First you should instruct json_decode to give you a pure array with $data = json_decode($response, TRUE); 首先,您应该指示json_decode使用$data = json_decode($response, TRUE);给您一个纯数组$data = json_decode($response, TRUE);
  • Then the result array is a list, and you have to access the first element as $data[0] 然后结果数组是一个列表,您必须以$data[0]访问第一个元素
  • The entry you want to access has the key postTitle , not post_title as your example code showed. 您要访问的条目具有键postTitle ,而不是示例代码所示的post_title (And unless that's a predefined constant won't work.) (并且除非这是一个预定义的常量,否则它将无法正常工作。)
  • And you need to put those array keys in quotes, like print $data[0]["postTitle"]; 并且您需要将这些数组键放在引号中,例如print $data[0]["postTitle"];

Turn up your error_reporting level for some development help. 调高error_reporting级别以获得一些开发帮助。

try this PHP code: 试试这个PHP代码:

//$post_data_response = file_get_contents("https://raw.github.com/gist/1245028/80e690bcbe6f1c5b46676547fbd396ebba97339b/Person_John.json");
//$PersonObject = json_decode($post_data_response);

// Get Person Object from JSON Source
$PersonObject = json_decode('{"ID":"39CA2939-38C0-4C4E-AE6C-CFA5172B8CEB","lastname":"Doe","firstname":"John","age":25,"hobbies":["reading","cinema",{"sports":["volley-ball","snowboard"]}],"address":{}}');

// Get data from Object
echo "Person ID => $PersonObject->ID<br>";
echo "Person Name => $PersonObject->firstname<br>";
echo "Person Lastname => $PersonObject->lastname<br>";  
echo "Person Age => $PersonObject->age<br>";
echo "Person Hobbies => " . $PersonObject->hobbies[0] . "<br>";     

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

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