无法从 Json 读取特定数据?

Can't read particular data from Json?

我有 json 数据,其中包含多个对象,我已将这些对象保存在数组中,但当我读取单个对象中的特定数据时,应用程序在下一行崩溃。这是我的代码:

NSString *myRequestString = [NSString stringWithFormat:@"getSavedSearches=yes&userId=40"];
// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: url]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response);


NSArray *a = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"Sunday: %@",[a description]);
NSDictionary *b = [a objectAtIndex:0];
NSLog(@"%@", [b valueForKey:@"SAVED_NAME"]); 

这是回复

{"0": {
                "EVENT_ID": "19",
                "NAME": "asdasd",
                "ADDRESS1": "asdasd",
                "ADDRESS2": "asdasd",
                "START_DATE": "2015-06-04",
                "END_DATE": "2015-06-12",
                "START_TIME": "2 PM",
                "END_TIME": "10 PM",
                "SAVED": "1",
                "SAVED_NAME": "hello",
                "UPDATED_BY": "New Event",
                "UPDATED_AT": "2015-06-25 14:13:43"
                }, "1": {
                "EVENT_ID": "20",
                "NAME": "asdasdasdasdasdasdasd",
                "ADDRESS1": "asdasd",
                "ADDRESS2": "",
                "START_DATE": "2015-05-13",
                "END_DATE": "2015-05-14",
                "START_TIME": "3 PM",
                "END_TIME": "4 PM",
                "SAVED": "1",
                "SAVED_NAME": "yellow",
                "UPDATED_BY": "New Event",
                "UPDATED_AT": "2015-05-25 14:14:04"
                }, "2": {
                "EVENT_ID": "22",
                "NAME": "Osama",
                "ADDRESS1": "add",
                "ADDRESS2": "",
                "START_DATE": "2015-05-07",
                "END_DATE": "2015-05-27",
                "START_TIME": "1 AM",
                "END_TIME": "3 PM",
                "SAVED": "1",
                "SAVED_NAME": "yo yo",
                "UPDATED_BY": "New Event",
                "UPDATED_AT": "2015-05-26 11:38:14"
                }, "3": {
                "EVENT_ID": "23",
                "NAME": "zxczxc",
                "ADDRESS1": "zxczxc",
                "ADDRESS2": "zxczxc",
                "START_DATE": "2015-05-12",
                "END_DATE": "2015-05-26",
                "START_TIME": "10 AM",
                "END_TIME": "2 PM",
                "SAVED": "1",
                "SAVED_NAME": "hahaha",
                "UPDATED_BY": "New Event",
                "UPDATED_AT": "2015-05-26 11:55:24"
                }, "4": {
                "EVENT_ID": "24",
                "NAME": "wqeqweqe",
                "ADDRESS1": "qweqwe",
                "ADDRESS2": "qweqwe",
                "START_DATE": "2015-05-05",
                "END_DATE": "2015-05-07",
                "START_TIME": "2 AM",
                "END_TIME": "9 AM",
                "SAVED": "1",
                "SAVED_NAME": "jajajaja",
                "UPDATED_BY": "New Event",
                "UPDATED_AT": "2015-05-26 12:53:21"
                }, "5": {
                "EVENT_ID": "25",
                "NAME": "uiooiuuio",
                "ADDRESS1": "uiouio",
                "ADDRESS2": "uiouio",
                "START_DATE": "2015-05-05",
                "END_DATE": "2015-05-13",
                "START_TIME": "7 AM",
                "END_TIME": "3 PM",
                "SAVED": "1",
                "SAVED_NAME": "asdasdasdasdasdasd",
                "UPDATED_BY": "New Event",
                "UPDATED_AT": "2015-05-26 12:58:28"
                }, "6": {
                "EVENT_ID": "26",
                "NAME": "asdasdasd",
                "ADDRESS1": "asdasdasdas",
                "ADDRESS2": "",
                "START_DATE": "2015-05-14",
                "END_DATE": "2015-05-13",
                "START_TIME": "9 AM",
                "END_TIME": "10 PM",
                "SAVED": "1",
                "SAVED_NAME": "xcvxcvxcv",
                "UPDATED_BY": "New Event",
                "UPDATED_AT": "2015-05-26 16:48:43"
                }, "7": {
                "EVENT_ID": "37",
                "NAME": "Osama",
                "ADDRESS1": "asdasd",
                "ADDRESS2": "asdasd",
                "START_DATE": "2015-05-04",
                "END_DATE": "2015-05-12",
                "START_TIME": "2 PM",
                "END_TIME": "10 PM",
                "SAVED": "1",
                "SAVED_NAME": "Osama",
                "UPDATED_BY": "New Event",
                "UPDATED_AT": "2015-06-02 17:07:44"
                }, "8": {
                "EVENT_ID": "39",
                "NAME": "asdas",
                "ADDRESS1": "asdasd",
                "ADDRESS2": "asdasd",
                "START_DATE": "2015-06-03",
                "END_DATE": "2015-06-12",
                "START_TIME": "7 AM",
                "END_TIME": "3 PM",
                "SAVED": "1",
                "SAVED_NAME": "asdasd",
                "UPDATED_BY": "New Event",
                "UPDATED_AT": "2015-06-05 18:23:19"
                }, "9": {
                "EVENT_ID": "40",
                "NAME": "asdasd",
                "ADDRESS1": "asdasd",
                "ADDRESS2": "asdasd",
                "START_DATE": "2015-06-03",
                "END_DATE": "2015-06-17",
                "START_TIME": "8 AM",
                "END_TIME": "3 PM",
                "SAVED": "1",
                "SAVED_NAME": "asdasdasdasd",
                "UPDATED_BY": "New Event",
                "UPDATED_AT": "2015-06-05 18:26:06"
                }, "10": {
                "EVENT_ID": "41",
                "NAME": "asdasd",
                "ADDRESS1": "asdad",
                "ADDRESS2": "as",
                "START_DATE": "2015-06-09",
                "END_DATE": "2015-06-24",
                "START_TIME": "2 PM",
                "END_TIME": "11 PM",
                "SAVED": "1",
                "SAVED_NAME": "qeqweqweqweqweqweqweqwewqeqweqweqwqwee",
                "UPDATED_BY": "New Event",
                "UPDATED_AT": "2015-06-05 18:28:14"
                }, "11": {
                "EVENT_ID": "42",
                "NAME": "asdasd",
                "ADDRESS1": "asdasd",
                "ADDRESS2": "qweqwe",
                "START_DATE": "2015-06-03",
                "END_DATE": "2015-06-03",
                "START_TIME": "9 AM",
                "END_TIME": "9 PM",
                "SAVED": "1",
                "SAVED_NAME": "iopiop",
                "UPDATED_BY": "New Event",
                "UPDATED_AT": "2015-06-05 18:29:22"
                }, "12": {
                "EVENT_ID": "43",
                "NAME": "asdasd",
                "ADDRESS1": "as",
                "ADDRESS2": "adad",
                "START_DATE": "2015-06-10",
                "END_DATE": "2015-06-18",
                "START_TIME": "8 AM",
                "END_TIME": "8 PM",
                "SAVED": "1",
                "SAVED_NAME": "bvnvnvnvbnvbnvbn",
                "UPDATED_BY": "New Event",
                "UPDATED_AT": "2015-06-05 18:37:36"
                }, "13": {
                "EVENT_ID": "44",
                "NAME": "asdad",
                "ADDRESS1": "asdasd",
                "ADDRESS2": "qweqwe",
                "START_DATE": "2015-06-03",
                "END_DATE": "2015-06-17",
                "START_TIME": "9 AM",
                "END_TIME": "4 PM",
                "SAVED": "1",
                "SAVED_NAME": "asdasdasd",
                "UPDATED_BY": "New Event",
                "UPDATED_AT": "2015-06-05 18:41:27"
                }}

但是我的应用程序崩溃了:NSDictionary *b = [a objectAtIndex:0]; 我需要读取每个对象的 "SAVED_NAME" 数据并将其保存到数组中。

那个顶级对象是一个字典;尝试:

NSDictionary *a = [NSJSONSerialization JSONObjectWithData:returnData
                                                  options:NSJSONReadingMutableLeaves
                                                    error:nil];
NSLog(@"Sunday: %@",[a description]);
NSDictionary *b = a[@"0"];

它不需要是字典,因为您使用的是字符串化数字作为键;但就目前而言,它是一本字典。

因为您获取的数据不是 Array,它是 NSDictionary,它有键-“0”,“1”,.....

所以试试-

NSDictionary *a = [NSJSONSerialization JSONObjectWithData:returnData                                             options:NSJSONReadingMutableLeaves
                                                    error:nil];

for(int i=0;i<[a count];i++)
{
    NSDictionary *b = [a valueForKey:[NSString stringWithFormat:@"%d",i]];
    NSLog(@"%@", [b valueForKey:@"SAVED_NAME"]); 

}

JSON 回复大多在字典里。所以你需要改变你的根对象意味着改变 NSArray 到 NSDictionary。它会很好地工作。