2010-07-02 111 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"];