Thursday, February 21, 2013

Json File parsing in IOS

NOTE : need some changes
json file (test.json)


{
"commands":[
                {"command" : "Home", "method":"homemethod""image" : "homemg" },
                {"command" : "Exit", "method":"exitmethod""image" : "exitimg" },
    ]

}



step 1 :
   reading file test.json

   NSString* path = [[NSBundle mainBundle] pathForResource:@"test"
                                                     ofType:@"json"];
    NSString* content = [NSString stringWithContentsOfFile:path
                                                  encoding:NSUTF8StringEncoding
                                                     error:NULL];


step 2 :
 nsstring to nsdata

NSData* data = [content dataUsingEncoding:NSUTF8StringEncoding];


step 3 :
dict from nsdata
NSMutableDictionary *dict=[NSJSONSerialization JSONObjectWithData:data
                                                       options:NSJSONReadingMutableContainers
                                                         error:&error];


step 4 :
  test 

for(NSDictionary *d in array){
        NSLog(@"---------------------------");
        NSLog(@" command -- %@",[d objectForKey:@"command"]);
        NSLog(@" method -- %@",[d objectForKey:@"method"]);
        NSLog(@" image -- %@",[d objectForKey:@"image"]);
        NSLog(@"---------------------------");        
    }
    
}




No comments:

Post a Comment