如何在uitableview中解析这个json?

时间:2013-05-24 05:09:41

标签: iphone ios objective-c json

我是iOS的新人。我想在我的json中跟UITableView进行解析,但我不知道该怎么做。我知道使用json进行NSURL解析,但是在这个直接json解析中,我遇到了问题。请帮助.. 这是:

NSString *url = @"{\"LinkResult\":{\"0\":{\"Name\":\"Veeva - Devon1\",\"ButtonDisplay\":\"Veeva\",\"PasswordSaving\":\"Yes\",\"Status\":\"Success\",\"Message\":\"No Error\", \"Identifiers\": {\"Identifier1Name\": \"Identifier1value\",\"Identifier2Name\": \"Identifier2value\",\"Identifier3Name\": \"Identifier3value\"},}}}";

请告诉我接下来的步骤......

6 个答案:

答案 0 :(得分:3)

这是您的JSON

的结构
{
    "LinkResult": {
        "0": {
            "Name": "Veeva - Devon1",
            "ButtonDisplay": "Veeva",
            "PasswordSaving": "Yes",
            "Status": "Success",
            "Message": "No Error",
            "Identifiers": {
                "Identifier1Name": "Identifier1value",
                "Identifier2Name": "Identifier2value",
                "Identifier3Name": "Identifier3value"
            }
        }
    }
}

使用任何在线生成器创建一个json文件,并将其添加到您的项目中。拥有保存dataSource的属性。

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Data"
                                                        ofType:@"json"];
    NSData *data = [[NSData alloc]initWithContentsOfFile:filePath];

    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data
                                                         options:0
                                                           error:nil];

    //A dictionary which will contain info all users
    self.users = dict[@"LinkResult"];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.users allKeys] count];;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    NSArray *sortedKeys = [[self.users allKeys]sortedArrayUsingSelector:@selector(compare:)];
    NSString *key = sortedKeys[indexPath.row];

    NSDictionary *user = self.users[key]; 

    cell.textLabel.text = user[@"Name"];
    cell.detailTextLabel.text = user[@"Status"];

    return cell;
}

Source Code

答案 1 :(得分:0)

您需要使用NSJSONSerialization类,例如:

NSData *json = [url dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
NSDictionary *identifiers = [dataDictionary objectForKey:@"Identifiers"];
// Do stuff with the data

希望这是有道理的。您可以使用以下方法从JSON字符串中提取元素:

[dataDictionary objectForKey:@"ElementName"];

答案 2 :(得分:0)

您需要将json字符串转换为NSDictionary对象才能在tableView中使用,下面的代码会将其转换为NSDictionary对象,然后您可以使用其键值相应地设置值。

- (void)viewDidLoad
{
    NSString *jsonString = @"{\"LinkResult\":{\"0\":{\"Name\":\"Veeva - Devon1\",\"ButtonDisplay\":\"Veeva\",\"PasswordSaving\":\"Yes\",\"Status\":\"Success\",\"Message\":\"No Error\", \"Identifiers\": {\"Identifier1Name\": \"Identifier1value\",\"Identifier2Name\": \"Identifier2value\",\"Identifier3Name\": \"Identifier3value\"},}}}";

    NSData *jsondata=[jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsondata options:0 error:nil];

    displaydata=[[dataDictionary valueForKey:@"LinkResult"] valueForKey:@"0"];
    NSMutableArray *mutableKeys=[[NSMutableArray alloc] initWithArray:[displaydata allKeys]];
    [mutableKeys removeObject:@"Identifiers"];
    keys=[[NSArray alloc] initWithArray:mutableKeys];

    NSLog(@"JSON DIct: %@", displaydata);
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [keys count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier=@"cellIdentifier";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text = [displaydata valueForKey:[keys objectAtIndex:indexPath.row]];
    return cell;
}

答案 3 :(得分:0)

解析你可以像这样使用nsdictionary ......

NSData *json = [url dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
    NSDictionary *identifiers = [dataDictionary objectForKey:@"Your key"];

并在tableview中显示,您可以使用

arr = [[JSONObject objectForKey:@“results”] objectForKey:@“Your key”];

// and set cell text label as -

cell.textLabel.text =[[arr objectAtIndex:indexPath.row] objectForKey:@"Your key"];

答案 4 :(得分:0)

    NSString *url = @"{\"LinkResult\":{\"0\":{\"Name\":\"Veeva - Devon1\",\"ButtonDisplay\":\"Veeva\",\"PasswordSaving\":\"Yes\",\"Status\":\"Success\",\"Message\":\"No Error\", \"Identifiers\": {\"Identifier1Name\": \"Identifier1value\",\"Identifier2Name\": \"Identifier2value\",\"Identifier3Name\": \"Identifier3value\"},}}}";

    NSData* jsonData = [url dataUsingEncoding:NSUTF8StringEncoding];

    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
    NSLog(@"json is %@", json);

    NSDictionary *LinkResult = [json valueForKey:@"LinkResult"];
    NSLog(@"LinkResult is %@", LinkResult);

    for (id key in [LinkResult allKeys]) {
        NSLog(@"Value for 0 index is %@", [LinkResult valueForKey:key]);

        NSLog(@"ButtonDisplay is %@", [[LinkResult valueForKey:key] valueForKey:@"ButtonDisplay"]);
        NSLog(@"Message is %@", [[LinkResult valueForKey:key] valueForKey:@"Message"]);
        NSLog(@"Name is %@", [[LinkResult valueForKey:key] valueForKey:@"Name"]);
        NSLog(@"PasswordSaving is %@", [[LinkResult valueForKey:key] valueForKey:@"PasswordSaving"]);
        NSLog(@"Status is %@", [[LinkResult valueForKey:key] valueForKey:@"Status"]);
    }

答案 5 :(得分:-5)

这是朋友

NSString *url = @"{\"LinkResult\":{\"0\":{\"Name\":\"Veeva - Devon1\",\"ButtonDisplay\":\"Veeva\",\"PasswordSaving\":\"Yes\",\"Status\":\"Success\",\"Message\":\"No Error\", \"Identifiers\": {\"Identifier1Name\": \"Identifier1value\",\"Identifier2Name\": \"Identifier2value\",\"Identifier3Name\": \"Identifier3value\"},}}}";

NSArray *arrComponents = [url componentsSeparatedByString:@","];
NSMutableArray *arrmFilter = [[NSMutableArray alloc] init];

for(int i = 0; i < [arrComponents count] ;i++)
{
    NSString *str = [arrComponents objectAtIndex:i];
    //str = [str stringByReplacingOccurrencesOfString:@":" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"{" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"}" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"0" withString:@""];

    [arrmFilter addObject:str];
}

NSLog(@"Filtered array = %@", arrmFilter);

你必须在`stringByReplacingOccurencesOfString中做一些改变 但这个vill提取JSON .... 希望这会有所帮助..