2010-07-02 40 views
8

Tôi có JSON đối tượng như thế này:Làm thế nào để phân tích đối tượng JSON trong iPhone SDK (XCode) sử dụng JSON-Khung

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

Tôi muốn để có được những mảng [0] Giá trị (2) và mảng [1] giá trị (clientId, clientName, công việc) bằng cách sử dụng JSON-Framework. Bạn có bất kỳ ý tưởng làm thế nào để làm điều đó?

+9

Xin lỗi nếu tôi nghe có vẻ thô lỗ, nhưng google cho 'iphone parse json', lần truy cập đầu tiên. Đó là một hướng dẫn tốt imoh. –

Trả lời

21

Giả sử bạn đã theo các instructions to install JSON-Khung vào dự án của bạn, dưới đây là cách bạn sử dụng nó (lấy từ 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. Không phải là [[items objectAtIndex: 1] objectForKey: @ "clientId"]); –

+0

Bạn, tất nhiên, chính xác. Tôi đã chỉnh sửa câu trả lời của mình. – deanWombourne

6

cảm ơn bạn cho câu trả lời của bạn, vấn đề của tôi được giải quyết, tôi sửa đổi một chút bit từ mã của bạn, dưới đây là:

// 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

Tôi có một API thời gian ở định dạng json như thế nào tôi có thể sử dụng điều này, xin vui lòng giúp tôi. jsont ({ "id": "ntp-a1.nict.go.jp", "nó": 1232963971,248, "st": 1344228610,961, "bước nhảy vọt": 34, "bên cạnh": 1341100800, " bước ": 1 }) –

0

cách lưu trữ dữ liệu này trong 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

Chúng ta cần 1 lớp, chúng ta hãy nói MyData.h và 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 
//------------------------------------- 

Để lưu trữ dữ liệu của chúng tôi:

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

thử này

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

Bạn có thể tạo một hệ thống phân cấp các điểm dữ liệu. Ví dụ: nếu bạn muốn nhận mảng bên trong của đối tượng JSON, bạn có thể truy cập vào mảng đó bằng cách sử dụng:

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

Hoặc, bạn có thể làm điều tương tự. Ví dụ: trong API Yelp, bạn được cung cấp một JSON của một doanh nghiệp. Đặt các doanh nghiệp này vào một mảng, bạn có thể truy cập các yếu tố khác nhau của đối tượng bằng cách thực hiện:

NSString *businessLocation = [businessArray objectAtIndex: indexOfBusinessInArray][@"location"][@"display_address"]; 
Các vấn đề liên quan