PHP检查JSON以查看它是否挂起和/或未挂起

时间:2016-06-06 19:23:17

标签: php json

我有一个JSON字符串。

  • 我想回应"等待"如果状态未决,
  • 我想回应一下#34; Active"如果状态是活动的,
  • 我想回应"无效"如果无效。

的Json

未决:

"{"result":"success","message":"License key details retrieved.","status":"pending","max_allowed_domains":"1","email":"","registered_domains":[],"date_created":"","date_renewed":"","date_expiry":""}"

主动:

"{"result":"success","message":"License key details retrieved.","status":"active","max_allowed_domains":"1","email":"","registered_domains":[],"date_created":"","date_renewed":"","date_expiry":""}"

无效:

"{"result":"error","message":"Invalid license key"}"

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您使用的是PHP,请首先在包含JSON字符串的变量上使用Error Message

之后,您应该能够检查数组中的值。

一个松散的例子:

$arr = json_decode($json_string);

if($arr['result'] == "error"){
    echo "Invalid";
}elseif($arr['result'] == "success" && $arr['status'] == "pending"){
    echo "Pending";
}elseif($arr['result'] == "success" && $arr['status'] == "active"){
    echo "Active";
}else{
    echo "what if it's something else??";
}