如何使用JSON-Framework在iPhone SDK(XCode)中解析JSON对象

时间:2010-07-02 11:36:57

标签: iphone xcode json parsing

我有这样的JSON对象:

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

我想使用JSON-Framework获取array [0] value(2)和array [1]值(clientId,clientName,job)。你知道怎么做吗?

6 个答案:

答案 0 :(得分:21)

假设您已将instructions to install JSON-Framework跟踪到您的项目中,以下是您使用它的方式(取自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"]);

答案 1 :(得分: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"]);
}

答案 2 :(得分: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;
}

答案 3 :(得分:1)

试试这个

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

答案 4 :(得分:1)

您可以创建数据点的层次结构。例如,如果要获取JSON对象的内部数组,可以使用以下命令访问它:

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

或者,你可以做类似的事情。例如,在Yelp API中,您将获得一个企业的JSON。将这些业务放在数组中,您可以通过执行以下操作来访问对象的不同元素:

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

答案 5 :(得分: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
}