2010-07-02 48 views
8

到解析JSON對象在iPhone SDK(的XCode)我有JSON對象是這樣的:如何使用JSON-框架

{ "data": 
    {"array": 
    ["2", 
     {"array": 
      [ 
      {"clientId":"1","clientName":"Andy","job":"developer"}, 
      {"clientId":"2","clientName":"Peter","job":"carpenter"} 
      ] 
     } 
    ] 
    }, 
"message":"MSG0001:Success", 
"status":"OK" 
} 

我想要得到的數組[0]的值(2)和陣列[1]使用JSON-Framework的值(clientId,clientName,job)。你有什麼想法如何做到這一點?

+9

對不起,如果我聽起來粗魯,但是對於iphone解析json,谷歌首先命中。這是一個很好的教程imoh。 – 2010-07-02 11:40:04

回答

21

假設您遵循了instructions to install JSON-框架到您的項目,這裏是你如何使用它(從the docs here拍攝):

// Parse the string into JSON 
NSDictionary *json = [myString JSONValue]; 

// Get the objects you want, e.g. output the second item's client id 
NSArray *items = [json valueForKeyPath:@"data.array"]; 
NSLog(@" client Id : %@", [[items objectAtIndex:1] objectForKey:@"clientId"]); 
+0

Typo。它不應該是[[item objectAtIndex:1] objectForKey:@「clientId」]); – 2010-11-03 19:27:29

+0

你當然是對的。我編輯了我的答案。 – deanWombourne 2010-11-04 11:52:22

6

感謝您的回答,我的問題解決了,我修改了一點從這裏是:

// Parse the string into JSON 
NSDictionary *json = [myString JSONValue]; 

// Get all object 
NSArray *items = [json valueForKeyPath:@"data.array"]; 
NSArray *array1 = [[items objectAtIndex:1] objectForKey:@"array"]; 
NSEnumerator *enumerator = [array1 objectEnumerator]; 
NSDictionary* item; 
while (item = (NSDictionary*)[enumerator nextObject]) { 
    NSLog(@"clientId = %@", [item objectForKey:@"clientId"]); 
    NSLog(@"clientName = %@",[item objectForKey:@"clientName"]); 
    NSLog(@"job = %@",  [item objectForKey:@"job"]); 
} 
+0

我有一個JSON格式的時間API我如何使用這個,請幫助我。 jsont({ 「ID」: 「ntp-a1.nict.go.jp」, 「它」:1232963971.248, 「ST」:1344228610.961, 「飛躍」:34, 「下一個」:1341100800, 「步驟「:1 }) – 2012-08-06 04:53:38

0

如何將這個數據存儲在NSMUtableArray?

while (item = (NSDictionary*)[enumerator nextObject]) { 
    NSLog(@"clientId = %@", [item objectForKey:@"clientId"]);//this 
    NSLog(@"clientName = %@",[item objectForKey:@"clientName"]);//this 
    NSLog(@"job = %@",  [item objectForKey:@"job"]);//this 
} 
1

我們需要1類,讓說MyData.h和MyData.m

//MyData.h 
@interface MyData : NSObject { 
    NSString *clientId; 
    NSString *clientName; 
    NSString *job; 
} 

@property (nonatomic, retain) NSString *clientId; 
@property (nonatomic, retain) NSString *clientName; 
@property (nonatomic, retain) NSString *job; 

@end 

//MyData.m 
@implementation MyData 

@synthesize clientId, clientName, job; 

- (void)dealloc{  
    [clientId release]; 
    [clientName release]; 
    [job release]; 
    [super dealloc]; 
} 

@end 
//------------------------------------- 

要保存我們的數據:

NSMutableArray *dataArray = [[NSMutableArray alloc]init]; 
while (item = (NSDictionary*)[enumerator nextObject]) { 
    MyData *aMyData = [[MyData alloc] init]; 
    aMyData.clientId = [item objectForKey:@"clientId"]; 
    aMyData.clientName = [item objectForKey:@"clientName"]; 
    aMyData.job  = [item objectForKey:@"job"]; 
    [dataArray addObject:aMyData]; 
    [aMyData release]; 
    aMyData = nil; 
} 
1

試試這個

while (item = (NSDictionary*)[enumerator nextObject]) { 
NSMutableArray *myArray = [[NSMutableArray alloc] init]; 
[myArray AddObject:((NSDictionary*)[enumerator nextObject])]; 
} 
1

你可以創建數據點的層次結構。例如,如果你想獲得一個JSON對象的內部陣列,您可以使用訪問:

NSArrray *objectArray = jsonObject[@"array"][@"2"][@"array"]; 

或者,你可以做類似的事情。例如,在Yelp API中,您提供了一個業務的JSON。將這些業務放置在一個陣列中,您可以通過以下方式訪問該對象的不同元素:

NSString *businessLocation = [businessArray objectAtIndex: indexOfBusinessInArray][@"location"][@"display_address"];