I have a codeigniter application returns some data from my database to a view. I'm trying to send it back as json data.
The problem is that the data that is returned it malformed. it looks like this:
({'2.5':"Admin1", '2.10':"Admin2"})
When I test this on jsonlint.com, it shows that this is not valid json. the '2.5' should be in double quotes, not single quotes. What I don't understand is that I'm calling json_encode on my data before I pass it to the view. My code in my controller looks like this:
public function getbuildings()
{
$buildings = array();
$branchID = $this->uri->segment(3);
$buildingforbranch = array();
$_locations = $this->racktables_model->get_locations();
//print_r($_locations);
foreach ($_locations as $location)
{
if ((isset($location['L2FullID'])) && (!array_key_exists($location['L2FullID'],$buildings))) {
$buildings[$location['L2FullID']] = $location['L2Location'];
}
}
foreach ($buildings as $key => $value)
{
$pattern = "/(".$branchID."\.\d)/i";
if (preg_match($pattern,$key))
{
$buildingforbranch[(string)$key] = $value;
}
}
header ('Content-Type: application/json; charset=UTF-8');
echo json_encode($buildingforbranch);
}
As you can see from the code, I've even tried casting $key explicitly to a string data type. But that doesn't seem to change anything. Any suggestions? Thanks.
EDIT 1
When I do a var dump on $buildingforbranch right before the header / json_encode() calls, I get the following results:
array(3) {
["2.5"]=>
string(7) "Admin 2"
["2.10"]=>
string(7) "Admin 1"
["2.11"]=>
string(3) "SB4"
}
It looks good here ... but when I do a console.log() and pass in the data from the controller, the browser shows the incorrectly formed json data.
EDIT 2 Here's what i'm trying to accomplish. I need to dynamically create a combo box when a user clicks on a control on my page. If the ajax call results in an empty array, I don't want to display the combo. Otherwise, I try to populate the combo box with the results of the ajax call. Everything is working, except for the part where I'm attempting to check the length of the json data. My app always displays a combo box regardless of what is sent back.
Here's the code:
$.ajax({
url:"<?php echo site_url('switches/getbuildings/');?>" + "/" + $selectedvalue,
type:'GET',
dataType:'json',
success: function(returnDataFromController) {
console.log("getbuildings ajax call successfull");
var htmlstring;
htmlstring="<select name='L2Locations' id='L2Locations'>";
htmlstring = htmlstring + "<option value='all'>All</option>";
//console.log(returnDataFromController);
var JSONdata=[returnDataFromController];
console.log(JSONdata);
if (JSONdata.length != 0)
{
for(var i=0;i<JSONdata.length;i++){
var obj = JSONdata[i];
for(var key in obj){
var locationkey = key;
var locationname = obj[key];
htmlstring = htmlstring + "<option value='" + locationkey + "'>" + locationname + "</option>";
} //end inner for
$('#l2locations').html(htmlstring);
}//end outer for
}
else {
//alert('i think undefined');
$('#l2locations').html('');
}
}//success
});//end ajax
If I call the page that is returning the json data directly, i get [] as the result for an empty array.