简体   繁体   English

js json.stringify到PHP json_decode

[英]js json.stringify to PHP json_decode

I am new to JSON. 我是JSON的新手。

In JS, I create an array of values like so: 在JS中,我创建了一个值数组,如下所示:

var arrFields = $("td>.frmInput").map(function(){
    return  {
        id: this.id,
        value: $(this).val()
    };
}).get();

I then AJAX them to the server like so: 然后,我将它们AJAX到服务器,如下所示:

$.ajax({
    type: "POST",
    url: "ajax/ax_all_ajax_fns.php",
    data: "Fields=" +JSON.stringify(arrFields),
    success: function(recd) {
        alert(recd);
    }
});

Note that there is a mixture of strings, plus the JSON.stringified (?) array. 请注意,字符串混合在一起,加上JSON.stringified(?)数组。 (There are additional string values sent, so data must remain as string.) (发送了其他字符串值,因此数据必须保留为字符串。)

On the PHP side, I need to turn the received Fields string into an associative array. 在PHP方面,我需要将收到的Fields字符串转换为关联数组。

Doing this: 这样做:

$jsonAsStr_Fields = $_POST['Fields'];
die($jsonAsStr_Fields);

Returns this text string in the alert() : alert()返回此文本字符串:

[{"id":"rateDriver","value":"Jacques Villeneuve"},{"id":"rateCar","value":"Chev"}]

Doing this: 这样做:

$arrFields = json_decode($jsonAsStr_Fields, TRUE);
$driver = $arrFields['rateDriver'];
$car = $arrFields['rateCar'];
$tire = $arrFields['rateTire'];
die('Driver: [' .$driver. '] Car: [' .$car. ']  Tire: [' .$tire. ']');

Returns this: 返回此:

Driver: [ ]  Car: [ ]  Tire: [ ]

How can I turn the $jsonAsStr_Fields string into an assoc array, and thereby output the correct values to my alert? 如何将$ jsonAsStr_Fields字符串转换为assoc数组,从而将正确的值输出到警报?

Do this instead for your creation of values: 这样做是为了创建值:

var arrFields = {};
$("td>.frmInput").each(function(){
    arrFields[this.id] = $(this).val();
});

This will create an object, when JSON-stringified, that looks like this: 这将在JSON字符串化时创建一个对象,如下所示:

{"rateDriver":"Jacques Villeneuve", "rateCar":"Chev"}

Which seems to be the format you want to use in your PHP code. 这似乎是您要在PHP代码中使用的格式。

You have an array of associative arrays and your arrays don't have the specified props, rateDriver for example is the value of the first array's element's id : 你有一个关联数组数组,你的数组没有指定的道具,例如rateDriver是第一个数组的元素id

$driver = $arrFields[0]['id'];
$car = $arrFields[1]['id'];

For seeing the array's contents you use the famous var_dump function. 要查看数组的内容,请使用着名的var_dump函数。

From the Author: 来自作者:

For those who haven't fully understood what solved this problem. 对于那些还没有完全明白什么解决了这个问题。

The underlying problem was that the stringified JSON was being modified en route (immed after hit Submit button en route to the PHP side) by the AJAX. 潜在的问题是,通过AJAX,字符串化的JSON在途中被修改(在点击提交按钮后到达PHP端)。 All quote marks were being escaped, which made it impossible for that string to work with json_encode. 所有引号都被转义,这使得该字符串无法与json_encode一起使用。

This was discovered by grabbing the value of the received data once it hit the PHP side: 这是通过在接收数据到达PHP端时获取接收数据的值来发现的:

$jsonAsStr_Fields = $_POST['Fields'];
die($jsonAsStr_Fields);

And alerting the received data in the AJAX success function: 并在AJAX成功函数中提醒收到的数据:

success: function(recd) {
    alert(recd);
}

Both of the above were described in the OP. OP中描述了上述两种情况。

However, because I assumed this was an unrelated problem, I "fixed" the string displayed in the alert() box when I posted the question. 但是,因为我认为这是一个无关的问题,所以当我发布问题时,我“修复”了alert()框中显示的字符串。 Lesson to be learned: don't help - just post what you actually see. 要吸取的教训:不要帮助 - 只发布你实际看到的内容。

It really displayed like this: 它真的显示如下:

{\"id\":\"rateDriver\",\"value\":\"Jacques Villeneuve\"}

but I wrote that it displayed like this: 但我写道它显示如下:

{"id":"rateDriver","value":"Jacques Villeneuve"}

Of course, the json_decode() PHP function had no idea what to do with the backslashes, so the string did not convert. 当然, json_decode() PHP函数不知道如何处理反斜杠,因此字符串没有转换。

Solution was to use str_replace() on the received JSON string over on the PHP side, to resolve the problem, like this: 解决方法是在PHP端对收到的JSON字符串使用str_replace()来解决问题,如下所示:

str_replace("\\", "", $_POST['Fields']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM