Ios 将存储在NSDictionary中的JSON数据中的应用程序名称存储在NSArray中
我以Ios 将存储在NSDictionary中的JSON数据中的应用程序名称存储在NSArray中,ios,json,api,nsarray,nsdictionary,Ios,Json,Api,Nsarray,Nsdictionary,我以JSON格式检索了以下响应表单服务器,并成功地将其存储在NSDictionary中 { "@companyName" = "MobileAPPSDeveloper"; "@generatedDate" = "8/6/13 2:41 AM"; "@version" = "1.0"; application = ( { "@apiKey" = 234FXPQB36GHFF2334H; "@creat
JSON
格式检索了以下响应表单服务器,并成功地将其存储在NSDictionary
中
{
"@companyName" = "MobileAPPSDeveloper";
"@generatedDate" = "8/6/13 2:41 AM";
"@version" = "1.0";
application = (
{
"@apiKey" = 234FXPQB36GHFF2334H;
"@createdDate" = "2013-03-01";
"@name" = FirstApp;
"@platform" = iPhone;
},
{
"@apiKey" = 3PF9WDY234546234M3Z;
"@createdDate" = "2013-02-01";
"@name" = SecondAPP;
"@platform" = iPhone;
},
{
"@apiKey" = NXGQXM2347Y23234Q4;
"@createdDate" = "2013-05-22";
"@name" = ThirdApp;
"@platform" = Android;
}
);
}
这是我用过的方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[self convertDataIntoDictionary:responseData];
}
-(void)convertDataIntoDictionary:(NSData*)data{
NSDictionary *dictionary;
if (data.length>0) {
NSError *parsingDataError;
dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&parsingDataError];
if (parsingDataError) {
// [ErrorAlert showError:parsingDataError];
NSString *recievedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data Conversion Error When Parsing: %@", recievedString);
}
}
NSLog(@"Data back from server\n %@",dictionary);
NSArray *applicationDetails = dictionary[@"application"];
}
我不能再这样下去了
我想获取阵列中所有应用程序的名称
任何帮助都将不胜感激。解析JSON的基础是: {}-dis表示字典
()-dis表示数组 因此,在您的
JSON
中,在根目录下有一个字典
,里面有一个键应用程序
,它包含一个数组
NSArray *applicationArray = [dictionary objectForKey:@"application"];
NSMutableArray *mutableAppNamesArray = [[NSMutableArray alloc]init];
在该数组中,每个索引处有三个NSDictionary
:
for(NSDictionary *dict in applicationArray){
NSString *appName = [dict objectForKey:@"@name"];
NSLog(@"app name : %@", appName);
[mutableAppNamesArray addObject:appName];
}
NSArray * appNamesArray = [NSArray arrayWithArray:mutableAppNamesArray];
我认为这不是JSON的正确格式。JSON对象通常包含“:”而不是“=” 示例:-
{
products_id: "476",
products_model: "XOLO Play",
products_price: "15499.0000",
products_image: "xolo-play-play-t1000-.jpg",
products_date_available: null,
products_last_modified: "2013-08-04 06:04:11",
products_date_added: "2013-08-04 06:03:10",
manufacturers_id: "17",
products_status: "1",
products_rating: null,
category_id: "11"
},
{
products_id: "18",
products_model: "Nokia Lumia 820",
products_price: "8990.0000",
products_image: "samsung-galaxy-ace-duos-gsm-mobile-phone-large-1.jpg",
products_date_available: null,
products_last_modified: "2013-07-30 03:45:28",
products_date_added: "2013-01-23 00:52:00",
manufacturers_id: "2",
products_status: "1",
products_rating: "3",
category_id: "11"
},
但如果您仍然需要对其进行解析,则可以尝试以下操作:
首先进入阵列
通过
NSDictionary*array=[allValues objectForKey:@“application”]代码>然后将每个索引添加到NSDictionary,然后将对象添加到key。如果是您的服务器响应,请尝试使用正确的JSON格式。您好,谢谢您的回答!!我会记住这一点,并尝试相应地转换数据。将在几分钟内更新!!NSArray*appNamesArray=[[NSArray alloc]init];NSArray没有addobject:method!!如果我改为NSMutableArray,它会给我错误[\uu NSArrayM insertObject:atIndex:]:object不能为nil'***第一次抛出调用堆栈:对不起,我的错误。我没有使用IDE。我编辑过。使用NSMutableArray而不是NSArray。如果您需要NSArray,您可以从NSMutableArray创建它。最终得到了答案!!我还编辑了答案!!谢谢PuneetGlad它成功了。我已经在最终答案中加入了您的编辑。快乐编码:)实际服务器响应为:@Puneet ans肯定会帮助您:)