1

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,

Ryan
  • 14,392
  • 8
  • 62
  • 102
CaribeSoft
  • 577
  • 1
  • 8
  • 25

2 Answers2

0

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

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

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

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);
superUntitled
  • 22,351
  • 30
  • 83
  • 110
  • Hi, yes, that's works for me. I'm receiving an array, how can in separate in variables ?. This is what I get: {"JSON":"\t{\n \"requestType\":\"TourListRequest\",\n \"data\":{\n \"ApiKey\":\"12345\",\n \"ResellerId\":\"999\",\n \"SupplierId\":\"999\",\n \"ExternalReference\":\"10051374722994001\",\n \"Timestamp\":\"2013-12-10T13:30:54.616+10:00\",\n \"Extension\":{\n \"any\":{\n \n }\n },\n \"Parameter\":{\n \"Name\":{\n \"0\":\" \"\n },\n \"Value\":{\n \n }\n }\n }\n}\n","URL":"api_test_json.php?test=JSON Tour List","Type":"JSON Tour List"} – CaribeSoft Apr 06 '14 at 07:12
  • Hi, this is what I'm receiving in my php – CaribeSoft Apr 06 '14 at 20:17
  • i updated my answer, it looks like you might need to use `stripslashes` on your data... take a look at this post for further info... http://stackoverflow.com/questions/7873259/decoding-json-array-of-objects-in-php-is-it-the-back-slashes – superUntitled Apr 07 '14 at 14:23