PHP:从jQuery解析JSON [英] PHP: parse JSON from jQuery

查看:83
本文介绍了PHP:从jQuery解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析一个简单的JSON字符串,但是我不习惯从PHP进行解析.为了进行测试,我写了一个页面来创建数据.将请求发送到PHP页面进行一些测试.

I'm trying to parse a simple JSON string, but I'm not used to do it from PHP. To do my test I've written a page where the data is created; sends request to PHP page to do some tests.

如何从PHP访问JSON数据并将其返回?

How can I access the JSON data from PHP and have it returned back?

来自jQuery的字符串:

The string from jQuery:

var json_str = '{"id_list":[{"id":"2","checked":"true"},{"id":"1","checked":"false"}]}';
/*'{
    "id_list": [
        {
            "id": "2",
            "checked": "true"
        },{
            "id": "1",
            "checked": "false"
        }
    ]
}'*/
$.post ("php_json_parser.php", { data_to_send:json_str }, function (data_back) {
    alert (data_back);
});

PHP页面:

<?php

$data_back = json_decode (stripslashes($_REQUEST['data_to_send']), true);
print $data_back->{'id_list'[0]["id"]}; //??? how can I access to properties?

?>

推荐答案

将第二个参数设置为true的json_decode将JSON项转换为php数组.

json_decode with the second parameter set to true turns the JSON item into a php array.

要访问示例中的元素,请使用

So to access the element in your example, you'de use

$data_back['id_list'][0]['id']

这篇关于PHP:从jQuery解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆