weixin_33725722 2015-08-28 10:46 采纳率: 0%
浏览 21

将json文件发布到PHP

I am trying to pass a json file to PHP using Jquery. when I check the response I get the error json_decode expects parameter 1 to be a string.

Here is my jquery routine

 $("#ProcessData").click(function(){ 
var cols = $("#tblGroup thead tr th").map(function(){ 
 return $(this).text(); }); 

var headers = cols; 

// Fetch the data from the table body 
var tableObject = $("#tblGroup tbody tr.tableClass").map(function(i){
 var row = {}; $(this).find('td').each(function(i){ 
 var rowName = headers[i]; 
row[rowName] = $(this).text(); 
}); 

return row; 

}).get(); 

// convert object to JSON 
JSON.stringify(tableObject); 

//now call ajax to pass info to php 

$.ajax({ 
url: 'php/ProcessOrder.php', 
data: {my_json_data: tableObject}, 
type: 'POST', 
async: false, 
dataType: 'json', cache:false 
}); 
});

here is my php script

<?PHP
//this is the layout of the json object
//Title:First Name:Surname:Group Name this will needamending as the json object builds

require("dbsettings.php");
$_Reference= $_POST["my_json_data"]; //this is a json object
// Loop through Array
$someArray = json_decode($_Reference, true); // Replace ... with your PHP Array

foreach ($someArray as $key => $value) {
 echo $value["Title"] . ", " . $value["First Name"] . ", " . $value["Surname"] . ", " . $value["Group Name"] . "<br>";
}

?>

here is my json object

{"Title":"Mr","First Name":"12","Surname":"12","Group Name":"as"}

Using php fiddle I created and tested this script which worked perfectly

<?php

$someJSON = '[{"name":"Jonathan Suh","gender":"male"},{"name":"William Philbin","gender":"male"},{"name":"Allison McKinnery","gender":"female"}]';


// Loop through Array
$someArray = json_decode($someJSON, true); // Replace ... with your PHP Array

foreach ($someArray as $key => $value) {
 echo $value["name"] . ", " . $value["gender"] . "<br>";
}

?>

One thing I did notice is my php fidle json file has [] the posted object doesn't is this what the error message refers too? Or Should I not JSON.stringfy() and just pass it as a string into php and use JSON_encode?

thanks for any assistance

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥20 SQL数据查询,子查询
      • ¥15 c++字符串分割问题
      • ¥15 关于#sql#的问题:没有用命令关闭cdc,手动把系统表开启的cdc右键删除了
      • ¥15 vue+uniapp
      • ¥15 android freedom
      • ¥15 使用自定义的类型代替内置类型可行吗
      • ¥15 关于STM32的SPI和ENDAT接口编码器通信的问题
      • ¥15 关于#pdfbox#生成的PDF文件正常,转图片中文乱码的问题,如何解决?
      • ¥15 ADS中有关DAC控件的使用问题
      • ¥15 win11如何运行geoserver