尝试解析 JSON 响应时获取 <null>

Posted

技术标签:

【中文标题】尝试解析 JSON 响应时获取 <null>【英文标题】:getting <null> when trying to parse JSON response 【发布时间】:2011-05-12 10:55:17 【问题描述】:

我在尝试在 UILabel 或控制台上显示 string 时遇到&lt;null&gt; 的问题,这是我的代码一步一步:

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

现在对于控制台和标签,我得到了&lt;null&gt;,尽管我已经在我的数据库中检查了它,但它不为空。这是由于 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;

【问题讨论】:

web服务中的json字符串是如何生成的?该代码在您的控制之下吗? responseString 包含null。这是 JSON 解析器(在 ios 设备上)将字符串解析为对象之前。 在数据库或生成 json 响应的服务器代码中检查? 那是 php 吗?我不知道如何在 PHP 中在发送之前检查响应,但你应该先在那里验证,如果没问题,那么问题可能在于 requestString 的设置方式。 很抱歉,我不熟悉 PHP,我不知道您使用的是哪种数据库,但大多数数据库允许“可空”字段,所以即使类型是“字符串”它可以为“null”(表示未提供值)。您可能想用 PHP 和 mysql (?) 标签提出一个新问题,以获得更好的答案。但我以为你在 db 中看到了这些字段的值? 【参考方案1】:

我不太了解 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。 不过,我希望这可以帮助你喜欢我!

【讨论】:

嗨,谢谢你的回答,但是没有任何改变,解析时总是生成&lt;null&gt; :) 好吧,我很抱歉...但是你为什么不使用一个框架,而不是使用一个数组然后 JSONValue 呢?也许我用 valueForKey 解决了:因为我使用的是 Stig Brautaset 的 JSON 框架... (stig.github.com/json-framework) ;)

以上是关于尝试解析 JSON 响应时获取 <null>的主要内容,如果未能解决你的问题,请参考以下文章

使用 SwiftyJSON iOS 解析 JSON 响应

在grails中解析JSON时如何获取真正的空值而不是JSONObject.NULL值

ReactJS:Rest API JSON 响应:如何解析

iOS:JSON 解析为 NSArray?

改造 API 调用返回 null

尝试获取 json 响应时出错 [Flutter]