2012-05-04 124 views
-1

好吧,我應該更好地解釋這個問題。我開發了一個iPhone應用程序來顯示數據。數據是使用php文件從Mysql數據庫中獲取的。JSON編碼問題ios 5

在這裏,我告訴PHP文件的代碼:

<?php 
header("text/html; charset=utf-8"); 
//Credenciales de la BBDD 
$db = "json"; 
$host = 'localhost'; 
$username = "dpbataller"; 
$password = '1234'; 

//Conectamos al servidor de la Base de datos 
$link = mysql_connect($host,$username,$password) or die("No se puede conectar"); 
//Seleccionamos la BBDD 
mysql_select_db($db) or die ("No se ha podido seleccionar a la base de datos"); 
mysql_set_charset('utf8', $link); 
//Lanzamos la consulta 
$consulta = mysql_query("SELECT id,nombre from personas"); 
//Creamos un array para almacenar los resultados 
$filas = array(); 
//Agregamos las filas devueltas al array 
while ($r = mysql_fetch_assoc($consulta)) { 
$filas[] = $r;  
} 
//Devolvemos el resultado 
echo json_encode($filas); 
?> 

在這一刻,如果我上運行的瀏覽器的腳本,他返回 [{ 「ID」: 「0」, 「農佈雷」: 「佩普\ u00e9」}]

此外,在Xcode項目我寫了這個代碼:

NSString *urlString = [NSString stringWithFormat:@"http://10.0.1.18/mysql_iphone/mostrar_personas.php"]; 

NSURL *url = [NSURL URLWithString:urlString]; 

NSData *data = [NSData dataWithContentsOfURL:url]; 

NSError *error; 

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
NSLog(@"%@", json); 

和控制檯,返回: ( { id = 0; nombre =「Pep \ U00e9」; } ) 像瀏覽器...這是我的問題,因爲我有口音的人noums ...

+3

你缺少您告訴我們您所遇到的問題的一部分。 – rckoenes

+0

這可能對於某些人來說是顯而易見的,你都搞不清出現編碼的Unicode字符,但我@rckoenes同意...請花一些時間來好好解釋一下你的問題......我 – Alladinian

+0

改寫了這個問題,抱歉 – dpbataller

回答

0

嗯,我猜你的問題是,你的JSON不工作,你需要使用NSDictionary不NSArray:

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

這應該解決它。

+0

它不起作用,我認爲php文件不能正確返回JSON編碼,可以嗎? – dpbataller

+0

看起來就是這個問題。 – shoughton123

0

你的JSON正在笨重,其實這是完全錯誤的。它應該看起來更像是這樣的:

[{ 
    "id":1, 
    "name":"Jos \u00e9" 
}, 
{ 
    "id":2, 
    "name":"David" 
} 
] 

我建議你與很多驗證的一個檢查JSON格式在那裏,例如像:http://json.parser.online.fr/

編輯 看來你得到經調試上當......

我試過如下:

NSError *error = nil; 
    NSData *data = [@"[{\"id\":\"0\",\"nombre\":\"Pep\u00e9\"}]" dataUsingEncoding:NSUTF8StringEncoding]; 

    NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 
    NSLog(@"%@", json); 

    NSDictionary *item = [json lastObject]; 
    NSString *nombre = [item objectForKey:@"nombre"]; 

    NSLog(@"%@", nombre); 

第一個NSLog將打印轉義字符,但提取的字符串實際上是以UTF8正確編碼的。

EDIT 2

你的代碼已經工作,你不需要改變任何東西

NSString *urlString = [NSString stringWithFormat:@"http://10.0.1.18/mysql_iphone/mostrar_personas.php"]; 

NSURL *url = [NSURL URLWithString:urlString]; 

NSData *data = [NSData dataWithContentsOfURL:url]; 

NSError *error; 

NSArray *items = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

// This will print all the names of people in your JSON. 
for (NSDictionary *item in items) 
{ 
    NSLog(@"nombre: %@", [item objectForKey:objectForKey:@"nombre"]); 
} 
+0

該信息由php腳本返回。如果我在瀏覽器上運行腳本,我可以看到這個[{「id」:「0」,「nombre」:「Pep \ u00e9」}] – dpbataller

+0

好的!此代碼有效,但...如何適應我的代碼?因爲我從URL獲取JSON對象。是否有可能?謝謝 – dpbataller

+0

好的!有用!但我需要將人員的姓名保存在數組中,而不是顯示它。 我該怎麼做? 非常感謝你,你正在幫助我很多.. – dpbataller