iOS解析内部Json
您好,我需要解析以下json,但是,我正在努力解析内部数组。我现在只打印每个内部数组,但我想打印每个标题,并将标题添加到数组中。谢谢你的帮助 JSONiOS解析内部Json,ios,json,nsarray,Ios,Json,Nsarray,您好,我需要解析以下json,但是,我正在努力解析内部数组。我现在只打印每个内部数组,但我想打印每个标题,并将标题添加到数组中。谢谢你的帮助 JSON {"nodes":[{ "node":{ "nid":"1420857", "title":"Title 1", "votes":"182", "popular":"True", "teaser":"Teaser 1" }}, {"node":
{"nodes":[{
"node":{
"nid":"1420857",
"title":"Title 1",
"votes":"182",
"popular":"True",
"teaser":"Teaser 1"
}},
{"node":{
"nid":"1186152",
"title":"Title 2",
"votes":"140",
"popular":"True",
"teaser":"Teaser 2"
}},
{"node":{
"nid":"299856",
"title":"Title 3",
"votes":"136",
"popular":"True",
"teaser":"Teaser 3"
}}
]}
Json解析器
NSError *error = nil;
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.somefilename.json"]];
if (jsonData) {
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if (error) {
NSLog(@"error is %@", [error localizedDescription]);
return;
}
NSArray *keys = [jsonObjects allKeys];
for (NSString *key in keys) {
NSLog(@"%@", [jsonObjects objectForKey:key]);
}
} else {
// Handle Error
}
只需键入:
NSArray *nodes = (NSArray*)[jsonObjects objectForKey:@"nodes"];
for (NSDictionary *node in nodes){
// do stuff...
}
返回id
(如-[objectForKey:
和-[objectAtIndex:
)的方法可以返回任何objective-c对象。您需要提前知道如何将其类型转换为什么,以便对其执行适当的操作。JSON被转换为NSObject等价物:
->object
NSDictionary
->array
NSArray
->string
NSString
->number
NSNumber
->boolean
NSNumber
->float
NSNumber
->null
NSNull
要区分不同的NSNUMBER,您必须调用相应的类型方法:
-[intValue]
,-[boolValue]
,-[floatValue]
。有关更多信息,请查看。您可以使用我的方法进行json解析
解析方法:
-(void)jsonDeserialize:(NSString *)key fromDict:(id)content completionHandler:(void (^) (id parsedData, NSDictionary *fromDict))completionHandler{
if (key==nil && content ==nil) {
completionHandler(nil,nil);
}
if ([content isKindOfClass:[NSArray class]]) {
for (NSDictionary *obj in content) {
[self jsonDeserialize:key fromDict:obj completionHandler:completionHandler];
}
}
if ([content isKindOfClass:[NSDictionary class]]) {
id result = [content objectForKey:key];
if ([result isKindOfClass:[NSNull class]] || result == nil) {
NSDictionary *temp = (NSDictionary *)content;
NSArray *keys = [temp allKeys];
for (NSString *ikey in keys) {
[self jsonDeserialize:key fromDict:[content objectForKey:ikey] completionHandler:completionHandler];
}
}else{
completionHandler(result,content);
}
}
}
NSData *content = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Sample" ofType:@"json"]];
NSError *error;
方法调用:
-(void)jsonDeserialize:(NSString *)key fromDict:(id)content completionHandler:(void (^) (id parsedData, NSDictionary *fromDict))completionHandler{
if (key==nil && content ==nil) {
completionHandler(nil,nil);
}
if ([content isKindOfClass:[NSArray class]]) {
for (NSDictionary *obj in content) {
[self jsonDeserialize:key fromDict:obj completionHandler:completionHandler];
}
}
if ([content isKindOfClass:[NSDictionary class]]) {
id result = [content objectForKey:key];
if ([result isKindOfClass:[NSNull class]] || result == nil) {
NSDictionary *temp = (NSDictionary *)content;
NSArray *keys = [temp allKeys];
for (NSString *ikey in keys) {
[self jsonDeserialize:key fromDict:[content objectForKey:ikey] completionHandler:completionHandler];
}
}else{
completionHandler(result,content);
}
}
}
NSData *content = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Sample" ofType:@"json"]];
NSError *error;
//要获取序列化的json数据
//获取名为GetInfo的键的数据
它工作正常,做的正是应该做的。有什么问题?你说的内部数组是什么意思?您发布的内容中只有一个数组,其他所有内容都是字典。你到底想解析什么?就像我需要打印循环中的每个标题一样,我已经尝试了很多方法,但不知道如何在我打印的内容中获取这些对象。刚刚编辑了我的问题很抱歉,我不清楚,但我很想打印循环中的每个标题,而不是您已经获取的每个数组。这是NSDictionary-->NSArray-->NSDictionary-->NSDictionary-->NSString。在do stuff注释中,我如何记录标题我尝试过的NSLog(@“%@,[node objectForKey:@“title]”);但是,这只会让我知道它是一个嵌套对象,所以您需要执行[(NSDictionary*)[NodeObjectForkey:@“node”]objectForKey:@“title”];啊,太好了,谢谢你的帮助,抱歉,我对这一点太陌生了:)关于数字,请看一下
[NSNumberFormatter numberFromString:
如果你不确定数字的格式,它会让你的生活变得简单,它只会将字符串解析为一个数值,你可以稍后决定如何使用它:int,float
,等等。