2011-05-12 93 views
2

我有試圖顯示在一個UILabel一個string或通過控制檯上的時候得到<null>問題的時候,這裏是我的代碼一步一步:越來越<null>試圖解析JSON響應

NSArray *array=[[request responseString]JSONValue]; 
for (int i=0; i<[array count]; i++) { 
NSDictionary *stationEnCours=[array objectAtIndex:i]; 
NSLog(@"%@",[stationEnCours objectForKey:@"ssiphone_adresse"]); 
} 
topStation.sstationAdress=[stationEnCours objectForKey:@"ssiphone_adresse"]; //global variable 
adresseStation.text=[NSString stringWithFormat:@"%@",topStation.sstationAdress];//display on label 

現在兩個控制檯和標籤我得到了<null>,雖然我在我的數據庫中檢查它不是null。是由於JSON解析規則?我該如何解決它? THX提前幫助:) 編輯: 這裏是我的迴應字符串:

2011-05-12 15:09:38.424 TopStation[1070:207] response string :[ 
{"ssiphone_idstation":"80","0":"80","ssiphone_etatstation":"true","1":"true","ssiphone_commerce":"true","2":"true","ssiphone_stationdelavage":"false","3":"false","ssiphone_typescarburants":"Sans Plomb 95,SP95-E10,Gazole,GPLc,GNV,","4":"Sans Plomb 95,SP95-E10,Gazole,GPLc,GNV,","ssiphone_joursdelasemaine":"7J\/7","5":"7J\/7","ssiphone_horaires":"24h\/24","6":"24h\/24","ssiphone_telephone":"0400221997","7":"0400221997","ssiphone_sensdecirculation":"(Sens >> Aix-en-Prov.\/Marseille)","8":"(Sens >> Aix-en-Prov.\/Marseille)","ssiphone_adresse":"A 51 - Aire de la Champouse","9":"A 51 - Aire de la Champouse","ssiphone_ville":"BOUC-BEL-AIR","10":"BOUC-BEL-AIR","ssiphone_departement":"13","11":"13","ssiphone_longitude":"5.39532","12":"5.39532","ssiphone_latitude":"43.4416","13":"43.4416","ssiphone_nomstation":"RELAIS TOTAL DE LA CHAMPOUSE","14":"RELAIS TOTAL DE LA CHAMPOUSE","ssiphone_voie":"A 51","15":"A 51","ssiphone_enseigne":"TOTAL","16":"TOTAL","ssiphone_codepostal":"13320","17":"13320","ssiphone_referencegps":"TOTAL DE LA CHAMPOUSE A 51","18":"TOTAL DE LA CHAMPOUSE A 51","distance":17.3}, 
{"ssiphone_idstation":"86","0":"86","ssiphone_etatstation":"true","1":"true","ssiphone_commerce":"true","2":"true","ssiphone_stationdelavage":"false","3":"false","ssiphone_typescarburants":"SP95-E10,Gazole,Super Gazole,GPLc,GNV,","4":"SP95-E10,Gazole,Super Gazole,GPLc,GNV,","ssiphone_joursdelasemaine":"7J\/7","5":"7J\/7","ssiphone_horaires":"24h\/24","6":"24h\/24","ssiphone_telephone":"0442591667","7":"0442591667","ssiphone_sensdecirculation":"","8":"","ssiphone_adresse":null,"9":null,"ssiphone_ville":"AIX-EN-PROVENCE","10":"AIX-EN-PROVENCE","ssiphone_departement":"13","11":"13","ssiphone_longitude":"5.43361","12":"5.43361","ssiphone_latitude":"43.5194","13":"43.5194","ssiphone_nomstation":"STATION BP","14":"STATION BP","ssiphone_voie":"","15":"","ssiphone_enseigne":"BP","16":"BP","ssiphone_codepostal":"13100","17":"13100","ssiphone_referencegps":"BP AIX EN PROVENCE","18":"BP AIX EN PROVENCE","distance":25.3},{"ssiphone_idstation":"89","0":"89","ssiphone_etatstation":"true","1":"true","ssiphone_commerce":"true","2":"true","ssiphone_stationdelavage":"false","3":"false","ssiphone_typescarburants":"SP95-E10,E85,Gazole,Super Gazole,GPLc,","4":"SP95-E10,E85,Gazole,Super Gazole,GPLc,","ssiphone_joursdelasemaine":"7J\/7","5":"7J\/7","ssiphone_horaires":null,"6":null,"ssiphone_telephone":"0442264052","7":"0442264052","ssiphone_sensdecirculation":"(Centre ville) (A8 sortie 31)","8":"(Centre ville) (A8 sortie 31)","ssiphone_adresse":"467 Avenue Henri Mauriat","9":"467 Avenue Henri Mauriat","ssiphone_ville":"AIX-EN-PROVENCE","10":"AIX-EN-PROVENCE","ssiphone_departement":"13","11":"13","ssiphone_longitude":"5.4661","12":"5.4661","ssiphone_latitude":"43.5157","13":"43.5157","ssiphone_nomstation":null,"14":null,"ssiphone_voie":"","15":"","ssiphone_enseigne":"TOTAL","16":"TOTAL","ssiphone_codepostal":"13090","17":"13090","ssiphone_referencegps":"TOTAL DE L'ARC","18":"TOTAL DE L'ARC","distance":24.9},{"ssiphone_idstation":"91","0":"91","ssiphone_etatstation":"true","1":"true","ssiphone_commerce":"true","2":"true","ssiphone_stationdelavage":"false","3":"false","ssiphone_typescarburants":"Sans Plomb 95,Sans Plomb 98,Gazole,Super Gazole,GPLc,GNV,","4":"Sans Plomb 95,Sans Plomb 98,Gazole,Super Gazole,GPLc,GNV,","ssiphone_joursdelasemaine":"7J\/7","5":"7J\/7","ssiphone_horaires":"L-S: 7h30-20h","6":"L-S: 7h30-20h","ssiphone_telephone":"0442958459","7":"0442958459","ssiphone_sensdecirculation":"(Dir.>> Aix Ouest) ","8":"(Dir.>> Aix Ouest) ","ssiphone_adresse":"Route de Berre - Jas de Bouffan","9":"Route de Berre - Jas de Bouffan","ssiphone_ville":"AIX-EN-PROVENCE","10":"AIX-EN-PROVENCE","ssiphone_departement":"13","11":"13","ssiphone_longitude":"5.41778","12":"5.41778","ssiphone_latitude":"43.5342","13":"43.5342","ssiphone_nomstation":null,"14":null,"ssiphone_voie":"","15":"","ssiphone_enseigne":"CASINO","16":"CASINO","ssiphone_codepostal":"13090","17":"13090","ssiphone_referencegps":"CASINO AIX EN PROVENCE","18":"CASINO AIX EN PROVENCE","distance":27.1}] 

編輯:

的響應編碼像這樣在服務器端:

$stationsFinales=array(); 
      $stationsFinales=$this->EpurationStations($stationsIdeales); 

      sendResponse(200,json_encode($stationsFinales)); 
      return true; 
+1

在Web服務中如何生成json字符串?該代碼是否在您的控制之下? – Anna 2011-05-12 15:16:32

+0

它生成得很好,沒有'' 當我嘗試在服務器端顯示響應時,字符串是字符串,數字是數字,正如我所期望的那樣,問題只在iPhone側解析:( – Luca 2011-05-12 15:23:31

+1

responseString包含'null',這是在JSON解析器(在iOS設備上)將字符串解析爲對象之前的__ – Anna 2011-05-12 15:31:31

回答

0

我不太瞭解JSON,但是當我在應用程序中使用它時,我通過使用valueForKey:而不是objectForKey:並嘗試使用此代碼:

NSArray *array = [[request responseString] JSONValue]; 

for (int i = 0; i < [array count]; i++) { 
    NSDictionary *stationEnCours = [array objectAtIndex:i]; 
    NSLog(@"%@", [stationEnCours valueForKey:@"ssiphone_adresse"]); 
    topStation.sstationAdress = [stationEnCours valueForKey:@"ssiphone_adresse"]; 
} 

adresseStation.text = [NSString stringWithFormat:@"%@", topStation.sstationAdress]; 

考慮這個答案只作爲測試,因爲正如我以前說過的,我不知道JSON很好。
但是,我希望這可以幫助你像我一樣!

+0

嗨,thx爲您的答案,但沒有任何更改,在解析:)時始終生成'' – Luca 2011-05-12 15:30:30

+0

好吧,我很抱歉...但不是使用數組,然後使用JSONValue,爲什麼你不使用框架?也許我用valueForKey解決了問題:因爲我使用Stig Brautaset的JSON Framework ...(http://stig.github.com/json-framework/);) – matteodv 2011-05-12 15:42:05