简体   繁体   中英

Converting JSON to PHP Array

I'm sending a JSON post to PHP that contains multiple items. My JSON looks like this:

 [
    {
        "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. Why is the decode not working?

Actually this JSON gets back slashes put in it when arriving to PHP. I took those out before posting here and also tried running it with 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. I decoded to PHP array like this:

$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.

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({
    url:[your url],
    data:data,
    ....
});

hope this will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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