Reputation: 61
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().
Upvotes: 0
Views: 105
Reputation: 61
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.
Upvotes: 1
Reputation: 1760
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
Upvotes: 0
Reputation: 453
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)
}
Upvotes: 0
Reputation: 5444
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);
Upvotes: 1