【问题标题】:JSON data extraction in PHPPHP中的JSON数据提取
【发布时间】:2018-05-03 06:25:25
【问题描述】:

我在服务器中有以下JSON data。我如何仅提取“SUCCESS”作为PHP 中的输出,其中SUCCESSStatus 的结果,它位于objectobject 内部。即 "status":"SUCCESS",

{  
   "response":{  
      "status":true,
      "statusCode":"0",
      "statusDescription":"Amount Debited",
      "data":{  
         "balanceamount":"528",
         "status":"SUCCESS",
         "statuscode":"0",
         "statusdescription":"Amount Debited",
         "debitedamount":"2",
         "orderid":"hj",
         "refId":"4450"
      }
   },
   "checksum":"23e97234820f5987189245e4216e89425472c2fe2c957749743b21d828efae67"
}

更新

$ch = curl_init();  // initiate curl
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);  // tell curl you want to post something
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string); // define what you want to post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in string format
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);     
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec ($ch); // execute

//echo "Request_Json= ".$data_string;
//echo "Response_Json=".$output;
echo $output;

这个$output 给了我上面的 JSON。 但是当我使用你的答案时,我的输出是空的。有什么我做错了吗?

【问题讨论】:

标签: php json server


【解决方案1】:

希望这对你有用:

json_decode 用于 json 字符串

$json= '{  
   "response":{  
      "status":true,
      "statusCode":"0",
      "statusDescription":"Amount Debited",
      "data":{  
         "balanceamount":"528",
         "status":"SUCCESS",
         "statuscode":"0",
         "statusdescription":"Amount Debited",
         "debitedamount":"2",
         "orderid":"hj",
         "refId":"4450"
      }
   },
   "checksum":"23e97234820f5987189245e4216e89425472c2fe2c957749743b21d828efae67"
}';


$arr = json_decode($json);

//print_r($arr);
echo $arr->response->data->status;
/*Output SUCCESS*/
die;

更新:

 $output = curl_exec ($ch); 
 $arr = json_decode($output );
 echo $arr->response->data->status;

更多:http://php.net/manual/en/function.json-decode.php

【讨论】:

  • 感谢您的回答@pradeep,请您看看我的新编辑并帮助我。
  • 用 $output 替换我的答案的 $json,如果 $output 与问题中显示的字符串相同,你一定会得到答案。
  • 没关系,您对此投反对票,但为什么要投反对票?请解释一下,可能是因为问题的正确答案-
  • 这不是一个错误的答案。但这并不能解决我的问题。不用担心,我没有投反对票,但还有其他人也提出了我的问题。 :(
  • 我认为这解决了你最初的问题,在更新问题后它改变了另一个问题的场景,顺便说一句没问题
【解决方案2】:
$output = '{  
   "response":{  
      "status":true,
      "statusCode":"0",
      "statusDescription":"Amount Debited",
      "data":{  
         "balanceamount":"528",
         "status":"SUCCESS",
         "statuscode":"0",
         "statusdescription":"Amount Debited",
         "debitedamount":"2",
         "orderid":"hj",
         "refId":"4450"
      }
   },
   "checksum":"23e97234820f5987189245e4216e89425472c2fe2c957749743b21d828efae67"
}';
 $json_result = json_decode($output);
echo $json_result->response->data->status;

【讨论】:

  • 您的答案与@pradeep 有何不同?如果不是,那么就给他投票。
  • 感谢您的回答@Rp9,请您看看我的新编辑并帮助我。
  • @GoGoaGone 立即查看
猜你喜欢
  • 1970-01-01
  • 2021-10-21
  • 2016-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-31
  • 2014-07-08
  • 2015-04-22
相关资源
最近更新 更多