利用NSJSONSerialization解析Json格式

ios中利用NSJSONSerialization来解析利用Json格式还是很容易的。直接把Jason数据当作Dictionary来用就可以了,然后要得到某个项的值就直接用NSDictionary的objectForKey方法就可以(通过键-Key,得到对应的Object-值即可),注意,通过objectForKey得到的值,可能还是一个字典(NSDictionary),然后再通过objectForKey得到下一级字典的值。


{

    data =     {

        "header_img" = "http://q.qlogo.cn/qqapp/103/1840390E30E7ADD1/100";

        nickname = syme;

        token = b7b0b6sd582df3ab61d1im815900;

        uid = 4;

    };

    info = "\U767b\U5f55\U6210\U529f";

    status = 1;

}


-(void)requestFinished:(ASIHTTPRequest *)request

{


   NSData *responseData = [requestresponseData];

   if (responseData){

        

       NSError *error =nil;

        

       NSDictionary *responseDict = [NSJSONSerializationJSONObjectWithData:responseData

                                                                    options:NSJSONReadingMutableLeaves

                                                                      error:&error];

        

       NSString *status = [responseDictobjectForKey:@"status"];

       NSString *info = [responseDictobjectForKey:@"info"];

        

       NSString *uid =nil;

       NSString *nickname =nil;

       NSString *token =nil;

       NSString *header_img =nil;

        

       NSDictionary *dataDic = [responseDictobjectForKey:@"data"];

        

       if (dataDic) {

            uid = [dataDicobjectForKey:@"uid"];

            nickname = [dataDicobjectForKey:@"nickname"];

            token = [dataDicobjectForKey:@"token"];

            header_img = [dataDicobjectForKey:@"header_img"];

        }

       


    }

    


或上级字典里某个键的值又是一数组,数组的元素又是字典,如下:


{"status":1,"info":"","data":[{"id":"1","name":"test1"},{"id":"1","name":"test2"},{"id":"1","name":"test3"},{"id":"1","name":"test4"},{"id":"1","name":"test5"},{"id":"1","name":"test6"}]}

-(void)getSongListFinished:(ASIHTTPRequest *)request

{

    

   NSData *responseData = [request responseData];

   if (responseData){


       NSError *error = nil;

       NSDictionary *responseDict = [NSJSONSerializationJSONObjectWithData:responseData

                                                                    options:NSJSONReadingMutableLeaves

                                                                      error:&error];

        

       NSLog(@"%@", responseDict);

        

       NSString *status = [responseDict objectForKey:@"status"];

       NSString *info = [responseDict objectForKey:@"info"];

        

       NSArray *dicArray = [responseDict objectForKey:@"data"];


       for (NSDictionary *oneSongDicin dicArray) {

            

           NSString *songID = [oneSongDic objectForKey:@"id"];

           NSString *songName  = [oneSongDic objectForKey:@"name"];

            

           NSLog(@"songid=%@, songName=%@", songID, songName);

         }

        

       int nStatus = [status intValue];

        

       if (nStatus == 0) {

            

        }

       else if(nStatus ==1)

        {

   

        }

       else{

   

        }

        

        [SVProgressHUDdismissWithSuccess:info];

    }

 

}





你可能感兴趣的:(iOS)