简体   繁体   English

如何解析特定的json数据响应?

[英]How do I parse a specific json data response?

Been yanking my hair out the past few days. 过去几天一直在拉扯我的头发。

I want to parse just a single piece of data from a string response using Get method. 我只想使用Get方法从字符串响应中解析出一条数据。

My php code I'm working with: 我正在使用的PHP代码:

 <?
    include "function.php";
    $request_rest->setMethod("GET");
    $result = $request_rest->execute();
    $response_status = $result[0];
    $json_response_data = $result[1];
    if ($response_status == "200") {
        echo $json_response_data;
    } else {
        echo $response_status ." - connection failure";
    }
    ?>

The results I get: 我得到的结果:

      {"data1":"value1",
       "data2":"value2",
       "data3":"value3",
       "data4":"value4",
       "data5":"value5"}

I only want to display "value3" for my output but instead I'm getting the full string response. 我只想在输出中显示“ value3”,但是却得到了完整的字符串响应。

If you know the key of the data you want (the data3 part) you can json_decode the json_response_data: 如果知道所需数据的键(data3部分),则可以json_decode json_response_data:

if ($response_status == "200") {
  $decoded = json_decode($json_response_data);
  echo $decoded['data3'];
}

Decode the JSON data and access it like any other array: 解码JSON数据并像访问其他数组一样访问它:

$data = json_decode($json_response_data, TRUE);
echo $data['data3'];

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

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