2017-02-07 135 views
1

我有下面的代码,只是想获取信息的内部下面和打印StatusPHP访问JSON响应并打印

"Email": { "Status": "in-queue" }

我真的新到PHP所以我对任何错误道歉,学习一门课。

{ 
    "APIResponse": { 
     "ResponseStatus": 1, 
     "Email": { 
      "EmailSid": "12893712893789", 
      "SentEmails": "[email protected]", 
      "Date": "2017-02-07 22:53:26", 
      "Subject": "700message", 
      "Status": "in-queue", 
      "TotalEmailSent": 1, 
      "TotalPrize": "0.0100", 
      "ApiVersion": "2" 
     } 
    } 
} 

我知道如何在JavaScript中做到这一点,但它似乎有点不同的PHP。

任何帮助表示赞赏感谢你们! :)

回答

1

下面是如何将JSON字符串转换成一个数组,并显示值的快速PHP例子:

$json = '{ "APIResponse": { "ResponseStatus": 1, "Email": { "EmailSid": "12893712893789", "SentEmails": "[email protected]", "Date": "2017-02-07 22:53:26", "Subject": "700message", "Status": "in-queue", "TotalEmailSent": 1, "TotalPrize": "0.0100", "ApiVersion": "2" } } }'; $array = json_decode($json, true); echo $array['APIResponse']['Email']['Status'];

注意每个子节点是如何成为关联数组中一个孩子。您还可以执行print_r($array);以查看本机PHP关联数组格式的整个结构。

+0

非常感谢你! – TXJ

+0

哦,克里斯先钉了它!投票:) – Stu

0
<?php 
$json = '{ 
    "APIResponse": { 
     "ResponseStatus": 1, 
     "Email": { 
      "EmailSid": "12893712893789", 
      "SentEmails": "[email protected]", 
      "Date": "2017-02-07 22:53:26", 
      "Subject": "700message", 
      "Status": "in-queue", 
      "TotalEmailSent": 1, 
      "TotalPrize": "0.0100", 
      "ApiVersion": "2" 
     } 
    } 
}'; 


// if this API response's JSON is being stored as a string type local variable decode it first. 

$decoded_json = json_decode($json, true); 

//Optional but helps when programing var_dump($decoded_json); Use this to help visualize the nested array variables you need to access. 

// Use each array key to walk into and access the desired value 

echo $decoded_json['APIResponse']['Email']['Status']; 


?> 

*编辑考虑到误差的基础上,在您的评论的问题:

,同时仍然确保首先解码JSON作为上面提到的,下面应该让你访问这些值。

echo $decoded_json['APIResponse']['Errors']['Error'][0]['Code']; 

echo $decoded_json['APIResponse']['Errors']['Error'][0]['Message']; 

最后,更多的建议,如果你曾经难倒。尝试一次访问一个数组,从最外层开始,一直工作,总能帮助我!

+0

这个数组里面呢?我想呼应' “守则”' ''' { “APIResponse”:{ “ResponseStatus”:0, “错误”:{ “错误”: { “代码”:“错误代码5001" , “消息”: “请输入正确格式的 '收件人' 电子邮件地址”, “MoreInfo”:[] } ] } } } ''' 最后一个问题,非常感谢你们! – TXJ