doukuilian8365
2014-04-06 04:57 阅读 169

提取json.stringify(数据)

I'm building an API, and I need a little help, to understand how can I parse data from JSON request, here is my code:

<textarea style="width: 100%; height: 300px;" id="request_json">
{
  "requestType":"TourListRequest",
  "data":{
  "ApiKey":"12345",
  "ResellerId":"999",
  "SupplierId":"999",
  "ExternalReference":"12345",
  "Timestamp":"2013-12-10T13:30:54.616+10:00",
  "Extension":{
  "any":{
      }
   },
  "Parameter":{
  "Name":{
    "0":" "
   },
  "Value":{
    }
   }
 }
}
 </textarea>

<script>
 function SubmitAPI() {
    var sendInfo = { 
      JSON    : $('#request_json').val(),
      URL     : $('#supplier_api_endpoint_JSON_Tour_List').val(),
      Type    : 'JSON Tour List'
    };

$('#response_json').html("Calling API...");
$.ajax({
       url: 'post_JSON.php', 
       type: "POST",
       data: JSON.stringify(sendInfo), // send the string directly
       success: function(response){
      var obj = jQuery.parseJSON( response );
    $('#response_json').html(response);
    $('#response_validation').html( obj.json_valid );
          },
          error: function(response) {
          $('#response_json').html(response);
           }
        }); 
   }
  </script>

So I need to know how to receive "JSON.stringify(sendInfo)" in my php script post_JSON.php

Any idea ?

Thank you in advance,

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dou1908 dou1908 2014-04-06 05:26

    I think you need to name you data string, something like...

    data: {info: JSON.stringify(sendInfo)},
    

    and in your php:

    $json_data = $_POST['info'];
    var_dump(json_decode($json_data, true));
    

    to get at that data using php, do something like:

    $postedData = json_decode($json_data, true); // turns json string into an object
    $requestType = $postedData['requestType']; 
    

    if you need to parse a returned json string with jquery you do something like this:

    var jsonStuff = jQuery.parseJSON( data );
    alert( jsonStuff.requestType);
    
    点赞 评论 复制链接分享
  • duanmei1922 duanmei1922 2014-04-06 05:03

    I don't know php, but I think you have to do the below.

    In the post_JSON.php, you can do: json_decode.

    点赞 评论 复制链接分享