1. 程式人生 > >IOS JSON解析之JSONKit使用

IOS JSON解析之JSONKit使用

JSONKit使用相當簡單。

將JSONKit.h和JSONKit.m拖到專案中。下載地址:https://github.com/johnezang/JSONKit/

下面程式碼:

    //string to dictionary
    NSString *resultStr = @"{\"name\": \"admin\",\"list\": [\"one\",\"two\",\"three\"]}";
    NSData* jsonData = [resultStr dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *resultDict = [jsonData objectFromJSONData];
    
    NSLog(@"name is :%@",[resultDict objectForKey:@"name"]);
    NSArray *list = [resultDict objectForKey:@"list"];
    for (NSString *str in list) {
        NSLog(@"list res:%@",str);
    }
   
    //dicttionary to string
    NSString *jsonStr = [resultDict JSONString];
    NSLog(@"temp is :%@",jsonStr);