Objective c 应用程序崩溃,因为NSArray objectforkey:目标C

Objective c 应用程序崩溃,因为NSArray objectforkey:目标C,objective-c,json,parsing,httprequest,Objective C,Json,Parsing,Httprequest,我试图用Objective C解析一些Json 我的问题是我得到了正确的json,但是当我尝试解析一些json时,我的应用程序崩溃了 // i will use a code from connect to DB tutorial NSString *strURL = [NSString stringWithFormat:@"http://www.ddproam.co.za/Central/Asset/AssetsWithSerial?Serial=S00000001"]; // to exe

我试图用Objective C解析一些Json

我的问题是我得到了正确的json,但是当我尝试解析一些json时,我的应用程序崩溃了

// i will use a code from connect to DB tutorial
NSString *strURL = [NSString stringWithFormat:@"http://www.ddproam.co.za/Central/Asset/AssetsWithSerial?Serial=S00000001"];

// to execute php code
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

// to receive the returend value
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];

NSLog(@"Login response:%@",strResult);

NSError *error;
//parse out the json data
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:dataURL //1
                                                     options:kNilOptions
                                                       error:&error];

NSArray* defineJsonData = [json objectForKey:@"AssetDesc"]; //2

NSLog(@"value: %@", defineJsonData); //3
这是我的json:

[{"AssetID":1,"AssetName":"Asset 1","AssetDesc":"This is a manually inserted Asset","AssetTypeID":1,"AssetTypeDesc":"This is a manually inserted Asset Type"}]

我正在尝试从字符串中取出AssetName。我一定是做错了什么。

整个事情是一个包含字典的数组,而不是一个包含数组的字典。。。 这是一种非常肮脏的获取所需值的方法—您希望编写比这更安全的内容。尝试使用返回的类之前,请先检查它的类型

NSArray* json = [NSJSONSerialization JSONObjectWithData:dataURL //1
                                                options:kNilOptions
                                                  error:&error];

NSDictionary* defineJsonData = [json lastObject]; //2

NSLog(@"value: %@", [defineJsonData objectForKey:@"AssetDesc"]); //3

整个事情是一个包含字典的数组,而不是一个包含数组的字典。。。 这是一种非常肮脏的获取所需值的方法—您希望编写比这更安全的内容。尝试使用返回的类之前,请先检查它的类型

NSArray* json = [NSJSONSerialization JSONObjectWithData:dataURL //1
                                                options:kNilOptions
                                                  error:&error];

NSDictionary* defineJsonData = [json lastObject]; //2

NSLog(@"value: %@", [defineJsonData objectForKey:@"AssetDesc"]); //3

非常感谢你为我澄清这一点@Wain。非常有帮助。非常感谢你帮我清理这些@Wain。非常有用。