iOS:json简单解析

json简单解析
-(NSString *)setJSON{
    
    NSDictionary *dTest = @{
        @"channel" : @{
            @"nodeId" : @"ELS-002-001",
            @"name" : @"A",
            @"hasChildren" : @1,
            @"parentId" : @"-1"
        },
        @"channels" :@""
    };
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:dTest options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return jsonStr;
}

-(void)parserJSON:(NSString *)jsonStr{
    
//    NSData *JSONData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"]];
    
    NSData *JSONData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    _dataDict = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:nil];
    
    NSDictionary *channel = [_dataDict objectForKey:@"channel"];
    BOOL hasChildren = [[channel objectForKey:@"hasChildren"] boolValue];
    
    NSLog(@"%@",_dataDict);
    if (hasChildren) {
        
    }
}

demo:https://github.com/ITHanYong/JSON.git

你可能感兴趣的:(iOS学习,ios,objective-c,xcode)