简体   繁体   English

将JSON转换为PHP数组

[英]Converting JSON to PHP Array

I'm sending a JSON post to PHP that contains multiple items. 我正在向包含多个项目的PHP发送JSON帖子。 My JSON looks like this: 我的JSON看起来像这样:

 [
    {
        "request": "submitTicket",
        "id": "3",
        "delivLoc": "1 COLORADO CITY",
        "estimatedBarrels": "123.0",
        "facilityID": "T666778",
        "highDegreeF": "0.0",
        "highOilFeet": "0"
    },
    {
        "request": "submitTicket",
        "id": "4",
        "delivLoc": "1 COLORADO CITY",
        "estimatedBarrels": "143.0",
        "facilityID": "T666778",
        "highDegreeF": "0.0",
        "highOilFeet": "0"
    },
    {
        "request": "submitTicket",
        "id": "5",
        "delivLoc": "1 COLORADO CITY",
        "estimatedBarrels": "122.0",
        "facilityID": "T666778",
        "highDegreeF": "0.0",
        "highOilFeet": "0"
    }
]

I've been trying to use json_decode() in PHP but it comes back null which causes the foreach loop to fail. 我一直试图在PHP中使用json_decode(),但它返回null,这会导致foreach循环失败。 Why is the decode not working? 为什么解码不起作用?

Actually this JSON gets back slashes put in it when arriving to PHP. 实际上,此JSON在到达PHP时会反斜线放入其中。 I took those out before posting here and also tried running it with stripslashes(). 在发布到这里之前,我将其取出来,还尝试使用stripslashes()运行它。

Try this.. 尝试这个..

$data ='[

{"request":"submitTicket","id":"3","delivLoc":"1 COLORADO CITY","estimatedBarrels":"123.0","facilityID":"T666778","highDegreeF":"0.0","highOilFeet":"0"},
{"request":"submitTicket","id":"4","delivLoc":"1 COLORADO CITY","estimatedBarrels":"143.0","facilityID":"T666778","highDegreeF":"0.0","highOilFeet":"0"},
{"request":"submitTicket","id":"5","delivLoc":"1 COLORADO CITY","estimatedBarrels":"122.0","facilityID":"T666778","highDegreeF":"0.0","highOilFeet":"0"}

]';

$jsonarray=json_decode($data,true);
print_r($jsonarray);

I figured it out after playing with everybody's suggestions. 我听了每个人的建议后就弄清楚了。 I had to remove the slashes from the post before trying to decode the JSON. 在尝试解码JSON之前,我必须从帖子中删除斜杠。 I decoded to PHP array like this: 我这样解码为PHP数组:

$data = json_decode(stripslashes($_POST['json']));

I suppose it was too late last night, and I some how overlooked this. 我想昨晚为时已晚,我对此有些疏忽。 Thanks everybody for your help and quick responses. 感谢大家的帮助和快速的回​​复。

Try this. 尝试这个。

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));

Output: 输出:

 object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

What i can see is the actual data which is passing may be not valid json. 我看到的是传递的实际数据可能是无效的json。

just assign the data in a variable and use like this 只需在变量中分配数据并像这样使用

var data = [{
        "request": "submitTicket",
        "id": "3",
        "delivLoc": "1 COLORADO CITY",
        "estimatedBarrels": "123.0",
        "facilityID": "T666778",
        "highDegreeF": "0.0",
        "highOilFeet": "0"
    },
    {
        "request": "submitTicket",
        "id": "4",
        "delivLoc": "1 COLORADO CITY",
        "estimatedBarrels": "143.0",
        "facilityID": "T666778",
        "highDegreeF": "0.0",
        "highOilFeet": "0"
    },
    {
        "request": "submitTicket",
        "id": "5",
        "delivLoc": "1 COLORADO CITY",
        "estimatedBarrels": "122.0",
        "facilityID": "T666778",
        "highDegreeF": "0.0",
        "highOilFeet": "0"
    }
]

and then pass this with the ajax(I assume you are using ajax) in data part something like this 然后在数据部分中将其与ajax(我假设您正在使用ajax)一起传递

$.ajax({
    url:[your url],
    data:data,
    ....
});

hope this will work 希望这会工作

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

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