JSON 格式有时不一样,如何从不同的 JSON 格式中提取信息?
JSON format sometimes not same, how to extract the info from the different JSON format?
我正在使用它从 Youtube
.
获取自动建议
["Tire",[["tired",0],["tired lyrics",0],["tired alan walker remix",0],["tired remix",0],["tired alan walker cover",0],["tired of being sorry enrique iglesias",0],["tire",0],["tired cover",0],["tiren mati kemaren full movie",0],["tired of talking",0]],{"k":1,"q":"raN20uYZUrouYBB7VsB396HlA88"}]
正如你从上面看到的从上面提取信息,我正在使用这段代码从 JSON array
.
NSString *json = nil;
NSScanner *scanner = [NSScanner scannerWithString:str];
[scanner scanUpToString:@"[[" intoString:NULL]; // Scan to where the JSON begins
[scanner scanUpToString:@"]]" intoString:&json];
NSLog(@"json before = %@", json);
//The idea is to identify where the "real" JSON begins and ends.
json = [NSString stringWithFormat:@"%@%@", json, @"]]"];
但有时 JSON array
可能是这种格式,如下所示。
["Like",[["likey",0,[131]],["likey twice lyrics",0,[3]],["likey dance",0,[3]],["likey dance practice",0,[3]],["likey live",0,[3]],["like i\u0027m gonna lose you",0],["like ooh ahh",0],["like a boss",0],["like a g6",0],["like a stone",0]],{"k":1,"q":"9DuLDtNkAUfZ2X9AVZN90t0Zxlw"}]
我如何提取上面示例中的信息,如 likey、likey twice 歌词?
NSScanner
并不是将原始 JSON 转化为有用数据的最佳 API。
最好使用原生 JSON API NSJSONSerialization
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
这里的 json
对象可以转换为 NSArray
。
其中数据是来自响应的原始数据。
另见 this answer
我正在使用它从 Youtube
.
["Tire",[["tired",0],["tired lyrics",0],["tired alan walker remix",0],["tired remix",0],["tired alan walker cover",0],["tired of being sorry enrique iglesias",0],["tire",0],["tired cover",0],["tiren mati kemaren full movie",0],["tired of talking",0]],{"k":1,"q":"raN20uYZUrouYBB7VsB396HlA88"}]
正如你从上面看到的从上面提取信息,我正在使用这段代码从 JSON array
.
NSString *json = nil;
NSScanner *scanner = [NSScanner scannerWithString:str];
[scanner scanUpToString:@"[[" intoString:NULL]; // Scan to where the JSON begins
[scanner scanUpToString:@"]]" intoString:&json];
NSLog(@"json before = %@", json);
//The idea is to identify where the "real" JSON begins and ends.
json = [NSString stringWithFormat:@"%@%@", json, @"]]"];
但有时 JSON array
可能是这种格式,如下所示。
["Like",[["likey",0,[131]],["likey twice lyrics",0,[3]],["likey dance",0,[3]],["likey dance practice",0,[3]],["likey live",0,[3]],["like i\u0027m gonna lose you",0],["like ooh ahh",0],["like a boss",0],["like a g6",0],["like a stone",0]],{"k":1,"q":"9DuLDtNkAUfZ2X9AVZN90t0Zxlw"}]
我如何提取上面示例中的信息,如 likey、likey twice 歌词?
NSScanner
并不是将原始 JSON 转化为有用数据的最佳 API。
最好使用原生 JSON API NSJSONSerialization
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
这里的 json
对象可以转换为 NSArray
。
其中数据是来自响应的原始数据。
另见 this answer