【问题标题】:extract json.stringify(data)提取 json.stringify(data)
【发布时间】:2014-04-06 04:57:46
【问题描述】:

我正在构建一个 API,我需要一些帮助,以了解如何从 JSON 请求中解析数据,这是我的代码:

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

所以我需要知道如何在我的 php 脚本 post_JSON.php 中接收“JSON.stringify(sendInfo)”

有什么想法吗?

提前谢谢你,

【问题讨论】:

标签: php jquery ajax json


【解决方案1】:

我认为您需要为数据字符串命名,例如...

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

在你的 php 中:

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

要使用 php 获取该数据,请执行以下操作:

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

如果你需要用 jquery 解析返回的 json 字符串,你可以这样做:

var jsonStuff = jQuery.parseJSON( data );
alert( jsonStuff.requestType);

【讨论】:

  • 嗨,是的,这对我有用。我正在接收一个数组,如何在变量中分开?这就是我得到的: {"JSON":"\t{\n \"requestType\":\"TourListRequest\",\n \"data\":{\n \"ApiKey\":\"12345\ ",\n \"ResellerId\":\"999\",\n \"SupplierId\":\"999\",\n \"ExternalReference\":\"10051374722994001\",\n \"时间戳\ ":\"2013-12-10T13:30:54.616+10:00\",\n \"分机\":{\n \"any\":{\n \n }\n },\n \ "参数\":{\n \"名称\":{\n \"0\":\" \"\n },\n \"值\":{\n \n }\n }\n }\n}\n","URL":"api_test_json.php?test=JSON Tour List","Type":"JSON Tour List"}
  • 嗨,这是我在我的 php
  • 我更新了我的答案,看来您可能需要在您的数据上使用stripslashes...查看这篇文章以获取更多信息...stackoverflow.com/questions/7873259/…
【解决方案2】:

我不懂php,但我认为你必须这样做。

post_JSON.php,你可以这样做:json_decode

【讨论】:

    猜你喜欢
    • 2015-05-25
    • 2014-10-10
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    相关资源
    最近更新 更多