猿问

从移动应用程序检索PHP上的json数据

我很难JSON从移动应用程序发送的发布请求中获取数据字段。


在客户端(移动应用程序)上,JSON可以使用模型类轻松对数据进行编码。


Future<http.Response> postRequest(CustomerData data) async {


  var url = 'http://xxxxxxxxxxxx/testSendData.php';

  var body = data.toJson();

  var response = await http.post(url, headers: {"Content-Type": "application/json"}, body: body);

  print("RESPONSE STATUS : ${response.statusCode}");

  print("RESPONSE BODY : ${response.body}");

  return response;

}

这部分工作良好,并生成JSON类似:


{  

   "creation":"12/01/2019",

   "status":"paid",

   "price":0.0,

   "items":[  

      {  

         "name":"Math books",

         "amount":"2"

      },

      {  

         "name":"Skates adult",

         "amount":"1"

      },

      {  

         "name":"Tools",

         "amount":"8"

      }

   ]

}

PHP服务器端(testSendData.php)不能很好地运行,我唯一设法获得的就是JSON数据本身,但无法获得不同的字段:


$jsonData = file_get_contents("php://input");

我尝试使用json_decode分别获取字段而没有成功,我也尝试创建一个php CustomerData class解码JSON的方法,但仍然无法正常工作。


获取JSON的各个字段的目的是将其中一些存储在数据库中


任何帮助或参考教程将不胜感激。


芜湖不芜
浏览 143回答 1
1回答

郎朗坤

我假设您将json数据放入此变量$ jsonData中&nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; echo "jsondata=>".$jsonData='{&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"creation":"12/01/2019",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"status":"paid",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"price":0.0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"items":[&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"name":"Math books",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"amount":"2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"name":"Skates adult",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"amount":"1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"name":"Tools",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"amount":"8"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;]&nbsp; &nbsp; &nbsp; }';&nbsp; &nbsp; &nbsp; $data1 = json_decode($jsonData, TRUE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<pre>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print_r($data1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($data1 as $key => $value1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($key=='creation'){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $value1."<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($key=='status'){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $value1."<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($key=='price'){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $value1."<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($key=='items'){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($value1 as $key1 => $value_deta) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print_r($value_deta);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答