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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3165290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:31:28  来源:igfitidea点击:

How to parsing JSON object in iPhone SDK (XCode) using JSON-Framework

iphonexcodejsonparsing

提问by inot

I have JSON object like this :

我有这样的 JSON 对象:

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

I want to get the array[0] value (2) and array[1] value (clientId, clientName, job) using JSON-Framework. Do you have any idea how to do that?

我想使用 JSON 框架获取数组 [0] 值 (2) 和数组 [1] 值(clientId、clientName、job)。你知道怎么做吗?

回答by deanWombourne

Assuming you've followed the instructions to installJSON-Framework into your project, here's how you use it (taken from the docs here) :

假设您已按照说明将JSON-Framework安装到您的项目中,以下是您如何使用它(取自此处的文档):

// 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"]);

回答by inot

thank you for your answer, my problem solved, I modify a little bit from your code, here are:

谢谢你的回答,我的问题解决了,我从你的代码中修改了一点,这里是:

// 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"]);
}

回答by inot

We need 1 class, let say MyData.h and MyData.m

我们需要 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
//-------------------------------------

To store our data :

存储我们的数据:

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;
}

回答by Radim Halfar

try this

尝试这个

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

回答by Jstngoulet

You can create a hierarchy of data points. For example, if you want to get the inner array of a JSON object, you can access it using:

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

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

Or, you can do something similar. For example, in the Yelp API, you are provided a a JSON of a business. Placing these businesses in an array, you can access different elements of the object by doing:

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

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

回答by prajakta

how to store this data in NSMUtableArray ??

如何将此数据存储在 NSMUtableArray 中?

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