Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

Retrieving json data on PHP from mobile app

I am having a hard time trying to get the JSON data fields, out of the post request sent from a mobile application.

On the client (mobile app) the JSON data is easily encoded using the model class.

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;
}

This part works well and generates a JSON like:

{  
   "creation":"12/01/2019",
   "status":"paid",
   "price":0.0,
   "items":[  
      {  
         "name":"Math books",
         "amount":"2"
      },
      {  
         "name":"Skates adult",
         "amount":"1"
      },
      {  
         "name":"Tools",
         "amount":"8"
      }
   ]
}

The PHP server side (testSendData.php) is not working well, the only I managed to get is the JSON data itself but not able to get the different fields:

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

I tried using json_decode to get fields individually without success, i also tried creating a php CustomerData class to decode JSON but still not working.

The purpose of getting the individual fields of the JSON is to store some of them in a DB

Any help or maybe a reference tutorial would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I assume you get a json data into this variable $jsonData

      <?php

        echo "jsondata=>".$jsonData='{  
         "creation":"12/01/2019",
         "status":"paid",
         "price":0.0,
         "items":[  
            {  
               "name":"Math books",
               "amount":"2"
            },
            {  
               "name":"Skates adult",
               "amount":"1"
            },
            {  
               "name":"Tools",
               "amount":"8"
            }
         ]
      }';

      $data1 = json_decode($jsonData, TRUE);
          echo "<pre>";
          print_r($data1);
          foreach ($data1 as $key => $value1) {

                if($key=='creation'){ 
                    echo $value1."<br>";
                }
                if($key=='status'){ 
                    echo $value1."<br>";
                }
                if($key=='price'){ 
                    echo $value1."<br>";
                }
                if($key=='items'){ 
                    foreach ($value1 as $key1 => $value_deta) {
                            print_r($value_deta);

                    }
                }

          }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...