用PHP解析SendGrid JSON
我有一些SendGrid API返回的简单JSON数据:用PHP解析SendGrid JSON,php,json,Php,Json,我有一些SendGrid API返回的简单JSON数据: { "message":"error", "errors":[ "some errors" ] } 我可以通过以下方式访问“消息”部分的内容: $txt = "{\"message\":\"success\"}"; $newtxt = json_decode($txt, true); echo $newtxt['message']; 这样可以,但我不知道如何访问“错误”部分的内容 抱歉,我意识到这
{
"message":"error",
"errors":[
"some errors"
]
}
我可以通过以下方式访问“消息”部分的内容:
$txt = "{\"message\":\"success\"}";
$newtxt = json_decode($txt, true);
echo $newtxt['message'];
这样可以,但我不知道如何访问“错误”部分的内容
抱歉,我意识到这可能是一个愚蠢的问题。如果您的JSON字符串是
$tst = '{
"message":"error",
"errors":[
"some errors"
]
}';
那么你需要做的就是
$j_array = json_decode($txt, true);
echo $j_array['message'];
echo $j_array['errors'][0];
更好的方法是在这个数组上循环
foreach ($j_array['errors'] as $error ) {
echo $error . '<br>';
}
或
foreach($jObj->errors as$error){
回显$error。“
”;
}
如果您的JSON字符串为
$tst = '{
"message":"error",
"errors":[
"some errors"
]
}';
那么你需要做的就是
$j_array = json_decode($txt, true);
echo $j_array['message'];
echo $j_array['errors'][0];
更好的方法是在这个数组上循环
foreach ($j_array['errors'] as $error ) {
echo $error . '<br>';
}
或
foreach($jObj->errors as$error){
回显$error。“
”;
}
查看一些基本的JSON+PHP指南;这里有很多,请查看一些基本的JSON+PHP指南;外面有很多