如何在iPhone中解析Json文件?
问题描述:
目前我正在iPhone应用程序中使用SBJson解析来自webservice的Json响应,但我没有解析它。如何在iPhone中解析Json文件?
这里我提到SBJson文件供您参考:
Array
(
[0] => 21
[s.no] => 21
[1] => Table243322
[table_id] => Table424242
[2] => arun123
[username] => aru22n123
[3] => sam101
[friend_user_name] => sa323m101
[4] => 0
[game_status] => 0
)
{"status":"1","error":{"0":"Table1684242538","table_id":"Table168424538","1":"KH,AC,10S,8D,2C,4H,JH,3S,10C,7D,3C,4D,7S","play_cards1":"KH,AC,10S,8D,2C,4H,JH,3S,10C,7D,3C,4D,7S","2":"QH,8H,2D,AS,KC,3D,6D,QD,9D,3H,2H,JD,AH","play_cards2":"QH,8H,2D,AS,KC,3D,6D,QD,9D,3H,2H,JD,AH","3":"10H","joker_card":"10H","4":"7C","open_deck":"7C","5":"AD,4C,9S,6S,QC,5C,8C,2S,4S,KS,5D,JS,RJ2,6H,JC,6C,9H,KD,10D,7H,QS,9C,5S,5H,RJ1,8S","closed_decks":"AD,4C,9S,6S,QC,5C,8C,2S,4S,KS,5D,JS,RJ2,6H,JC,6C,9H,KD,10D,7H,QS,9C,5S,5H,RJ1,8S"}}
我试图解析这一点,但价值来自空:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *jsonStr1 = [[NSString alloc] initWithData:mutaebleData encoding:NSUTF8StringEncoding];
NSLog(@"jsonStr1 : %@", jsonStr1);
SBJSON *Json = [[SBJSON alloc]init];
NSDictionary *dic= (NSDictionary *) [Json objectWithString:jsonStr1 error:nil];
// dic = null, dic value comes always null
}
如何分析呢?请帮我提前
答
感谢未来的版本中使用苹果的库/类(只是一个建议),以确保您的应用程序。如果您使用NSJSONSerialization的解决方案会像下面..
// 1) data is an ivar of NSData
// 2) i used delegate methods of NSURLConnection to fetch data from server
NSError *error=nil;
id result=[NSJSONSerialization JSONObjectWithData:data options:
NSJSONReadingMutableContainers error:&error];
// use result as how you want. you can use NSDictionary too instead of id ..
感谢您的回复 – SampathKumar
如果答案是有用的请你能接受吗? – ilhnctn
感谢您的帮助 – SampathKumar