2013-10-10 30 views
-1

我從兩個不同的Web服務獲取響應數組。但是對於相同的方法。問題是,有兩種不同的Web服務給了我不同的迴應。這些都是那些陣列如何檢查對象是一個字符串或ios中的數組

(NSMutableArray *) $2 = 0x003e9210 <__NSArrayM 0x3e9210>(

{ 

addedOn = "03/09/2013"; 

album = "Surendra Perera"; 

artistGroup = Female; 

artists = "Surendra Perera"; 

bpm = 0; 

categories = "Love Songs"; 

duration = "250.00"; 

energy = ""; 

era = Millenium; 

extroTime = "0.00"; 

extroType = ""; 

genders = ""; 

id = 50; 

imageUrl = "http://sample.com/CloudMusic/Images/sngfile.png"; 

introTime = "0.00"; 

language = Sinhala; 

lyrics = ""; 

mediaUrl = "http://sample.com/CloudMusic/Music/0476/50.mp3"; 

moods = Lonely; 

movie = ""; 

musicLabel = Evoke; 

musician = ""; 

sDuration = "00:04:10"; 

soundCodes = ""; 

tempos = ""; 

textures = ""; 

thumbUrl = "http://sample.com/CloudMusic/Images/sngfile.png"; 

title = "Mee Mai Gaha"; 

writer = ""; 

year = ""; 

} 

) 

另一種是

addedOn = "19/09/2013"; 

albumName = Massina; 

artists =  (

      { 

     artistGroup = 0; 

     description = "<null>"; 

     id = 290; 

     imageUrl = "<null>"; 

     name = Daddy; 

     noOfSongs = 0; 

     thumbUrl = "<null>"; 

    } 

); 

duration = "260.00"; 

id = 2575; 

imageUrl = "http://sample.com/CloudMusic/Images/sngfile.png"; 

mediaUrl = "http://sample.com/CloudMusic/Music/0905/2575.mp3"; 

sDuration = "00:04:20"; 

songMoods =  (

      { 

     id = 3; 

     name = Sad; 

     noOfSongs = 0; 

    } 

); 

thumbUrl = "http://sample.com/CloudMusic/Images/sngfile.png"; 

title = "Aai Kale"; 

year = ""; 

} 

) 

我想要做的就是赤此藝術家陣列。我該如何檢查這個藝術家是以數組還是字符串形式出現的。請幫幫我。

感謝

回答

2

這將illustarate在_recievedData您的問題 獲取數據RX然後檢查對象的類。

id object = [NSJSONSerialization 
       JSONObjectWithData:_recievedData 
       options:kNilOptions 
       error:&error]; 
if (error) 
{ 
    NSLog(@"Error in rx data:%@",[error description]); 
} 
if([object isKindOfClass:[NSString class]] == YES) 
{ 
    NSLog(@"String rx from server"); 
} 
else if ([object isKindOfClass:[NSDictionary class]] == YES) 
{ 
    NSLog(@"Dictionary rx from server"); 
} 
else if ([object isKindOfClass:[NSArray class]] == YES) 
{ 
    NSLog(@"Array rx from server"); 
} 
+3

如果'object'爲'nil',則只能檢查'error'。 – rmaddy

0

這應該對你有所幫助。

id object = [responseData objectForKey:@"artists"]; 

if ([object isMemberOfClass:[NSString class]]) { 
    // do the stuff for NSString 
} 
else if ([object isMemberOfClass:[NSArray class]]) { 
    // do the stuff for NSArray 
} 
相關問題