I have data stored in mysql database, the data is encoded in ascii because of some special characters, i am trying to fetch data for iphone application, the json format is correct but due to special characters it returned me null 42756666616c6f205068696c6c79e28099732077696e67732c20636865657365737465616b7320616e64206d6f7265 this is stored in mysql桌子虽然实际上是Buffalo Philly 的翅膀、奶酪 牛排等等,但我尝试了 html_entity_decode、html2text 但没有人帮助我,请帮助我,我真的被困住了。
这是我如何从 db 获取数据并以 json 格式打印的代码:
$sql = "SELECT p.product_id,p.image,p.model,d.name,d.product_id,d.language_id FROM product AS p ,product_description AS d
WHERE
d.product_id = p.product_id
AND
d.language_id = 1
ORDER BY p.date_added
limit 10
";
$rs = mysql_query($sql);
$data = array();
$products = '{"products":{';
$num = mysql_num_rows($rs);
while($d = mysql_fetch_array($rs)){
$image = new SimpleImage();
$image->load($path.$d['image']);
$image->resize(55,55);
$img = strtotime("now").$count;
$image->save("images/".$img.'.jpg');
$pimage = "$img_url/$img.jpg";
$name = html_entity_decode($d['name'],ENT_QUOTES, 'UTF-8');
$highlight = html_entity_decode($d['model'],ENT_QUOTES, 'UTF-8');
if($count < $num){
$products .= '"'.$d["product_id"].'":[
{
"id":"'.$d["product_id"].'",
"name":"'.$name.'",
"image_url":"'.$pimage.'",
"highlight":"'.$highlight.'"
}
],';
}else{
$products .= '"'.$d["product_id"].'":[
{
"id":"'.$d["product_id"].'",
"name":"'.$d["name"].'",
"image_url":"'.$pimage.'",
"highlight":"'.$highlight.'"
}
]';
}
}
$products .='}}';
print($products);
对象-c代码是:
NSData *data = [NSData dataWithContentsOfURL:soURL];
NSError *error;
productsRaw = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
products = [productsRaw objectForKey:@"products"];
productKeys = [products allKeys];