简体   繁体   English

使用iOS中的objectForKey使用NSJSONSerialization类解析json

[英]Parse json with NSJSONSerialization class using objectForKey in iOS

I am new in iOS development. 我是iOS开发的新手。 I use this code to connect to my REST Web Service and fetch data in Json format. 我使用此代码连接到我的REST Web服务并以Json格式获取数据。

    NSString *url=@"URL_Address";

    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];

    NSURLResponse *resp = nil;
    NSError *err = nil;

    NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];

//    NSString * theString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 
//    NSLog(@"response: %@", theString);

    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: response options: NSJSONReadingMutableContainers error: &err];

    if (!jsonArray) {
        NSLog(@"Error parsing JSON: %@", err);
    } else {
        for(NSDictionary *item in jsonArray) {
            NSLog(@" %@", item);
            NSLog(@"---------------------------------");
        }
    }

Now I want to seperate them via objectForKey . 现在我想通过objectForKey分隔它们。 I used this code inside the loop : 我在循环中使用了这段代码:

NSString *name = [item objectForKey:@"name"];

It does not work. 这是行不通的。 I got this error: 我收到了这个错误:

2012-07-31 12:48:38.426 LearningAFNetworking[390:f803] -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x6844460
2012-07-31 12:48:38.428 LearningAFNetworking[390:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x6844460'
*** First throw call stack:
(0x13c8022 0x1559cd6 0x13c9cbd 0x132eed0 0x132ecb2 0x2f40 0x2bf8 0xd9a1e 0x38401 0x38670 0x38836 0x3f72a 0x290b 0x10386 0x11274 0x20183 0x20c38 0x14634 0x12b2ef5 0x139c195 0x1300ff2 0x12ff8da 0x12fed84 0x12fec9b 0x10c65 0x12626 0x254d 0x24b5 0x1)
terminate called throwing an exception(lldb)

can you help me? 你能帮助我吗?

You have to access the 0 item of item array: 您必须访问item数组的0项:

NSArray *mainArray = [item objectAtIndex:0];
(NSDictionary *obj in mainArray) {
     NSString *name = [obj objectForKey:@"name"];
}

When you got this error: -[__NSArrayM objectForKey:] , you need to realize that the object that you're trying to access isn't a dictionary. 当您收到此错误时: -[__NSArrayM objectForKey:] ,您需要意识到您尝试访问的对象不是字典。 It's an array ( __NSArrayM ), so you have to first access the 0 index, and then starting exploring the dictionary. 它是一个数组( __NSArrayM ),因此您必须首先访问0索引,然后开始浏览字典。

Take a look at this amazing tutorial, from Ray Wenderlich, that explains everything about crashes. 看一下来自Ray Wenderlich的这个惊人的教程,它解释了崩溃的一切。

As Alberto said, you assumed that item is an NSDictionary while in reality is an NSArray containing multiple dictionaries. 正如Alberto所说,你认为该项目是一个NSDictionary而实际上是一个包含多个词典的NSArray Now for the drill-down part you can use valueForKeyPath as shown below. 现在,对于下钻部分,您可以使用valueForKeyPath ,如下所示。 So your for loop should be something like this: 所以你的for循环应该是这样的:

NSArray *items = [jsonArray objectAtIndex:0];

for (NSDictionary *item in items){
    NSString *name = [item valueForKey:@"name"];
    // You can also get nested properties like this
    NSString *projectName = [item valueForKeyPath:@"project.name"]; 
} 

Another useful thing to know is that if you'd like to get all 'name' values for example, you could use valueForKey on your items array, which would automatically call valueForKey on each dictionary for you. 另一个有用的事情是,如果你想获得所有'name'值,你可以在items数组上使用valueForKey ,它会自动为每个字典调用valueForKey Example: 例:

NSArray *items = [jsonArray objectAtIndex:0];
NSArray *names = [items valueForkey:@"name"];

From the error and from the log output of your code it looks like your are trying to execute the selector objectForKey on a NSArray . 从错误和代码的日志输出看起来你正试图在NSArray上执行选择器objectForKey Try to change NSString *name = [item objectForKey:@"name"]; 尝试更改NSString *name = [item objectForKey:@"name"]; to NSString *name = [[item objectAtIndex: 0]objectForKey:@"name"]; to NSString *name = [[item objectAtIndex: 0]objectForKey:@"name"];

这段代码可以帮助您从NSArray获取类型字典的第一个对象

[[jsonArray objectAtIndex:0]objectForKey:@"object-key"];
-(void)clientServerCommunication
{
    NSURL *url = [NSURL URLWithString:@"http://182.72.122.106/iphonetest/getTheData.php"];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:req delegate:self];
    if (connection)
    {
        webData = [[NSMutableData alloc]init];
    }
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];

    /*Third party API
     NSString *respStr = [[NSString alloc]initWithData:webData encoding:NSUTF8StringEncoding];
     SBJsonParser *objSBJson = [[SBJsonParser alloc]init];
     NSDictionary *responseDict = [objSBJson objectWithString:respStr]; */
    resultArray = [[NSArray alloc]initWithArray:[responseDict valueForKey:@"result"]];
    NSLog(@"resultArray: %@",resultArray);
    [self.tableView reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    custcell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];



    if(custcell==nil)
    {

        custcell=[[[NSBundle mainBundle]loadNibNamed:@"custumCell" owner:self options:nil]objectAtIndex:0];

    }


    custcell.persnName.text=[[arr objectAtIndex:indexPath.row]valueForKey:@"Name"];
    //else if (objsegment.selectedSegmentIndex==1)
    //{
    custcell.persnAge.text=[[arr objectAtIndex:indexPath.row]valueForKey:@"Age"];

    [sortcontrol addTarget:self action:@selector(SegmentbtnCLK:) forControlEvents:UIControlEventValueChanged];

    //objsegment.selectedSegmentIndex=0;
    // }

    return custcell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here, for example:
     //Create the next view controller.
    detailViewController *detailViewController1 = [[detailViewController alloc]initWithNibName:@"detailViewController" bundle:nil];

 //detailViewController *detailViewController = [[detailViewController alloc] initWithNibName:@"detailViewController" bundle:nil];

 // Pass the selected object to the new view controller.

 // Push the view controller.
 detailViewController1.nextDict = [[NSDictionary alloc]initWithDictionary:[resultArray objectAtIndex:indexPath.row]];
 [self.navigationController pushViewController:detailViewController1 animated:YES];

    // Pass the selected object to the new view controller.

    // Push the view controller.
  //  [self.navigationController pushViewController:detailViewController animated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    empName.text=[nextDict valueForKey:@"name"];
    deptlbl.text=[nextDict valueForKey:@"department"];
    designationLbl.text=[nextDict valueForKey:@"designation"];
    idLbl.text=[nextDict valueForKey:@"id"];
    salaryLbl.text=[nextDict valueForKey:@"salary"];
    NSString *ImageURL = [nextDict valueForKey:@"image"];
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
    image.image = [UIImage imageWithData:imageData];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM