-5

我是新来的iOS。我想解析以下内容,但jsonUITableView不知道该怎么做。我知道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\"},}}}";

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

4

6 回答 6

3

这是您的 JSON 的结构

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

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

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

源代码

于 2013-05-24T05:40:23.383 回答
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"];
于 2013-05-24T05:20:38.197 回答
0

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

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

并在表格视图中显示它,您可以使用

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

// and set cell text label as -

cell.textLabel.text =[[arr objectAtIndex:indexPath.row] objectForKey:@"Your key"];
于 2013-05-24T05:29:43.003 回答
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;
}
于 2013-05-24T05:27:58.153 回答
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"]);
    }
于 2013-05-24T05:55:04.020 回答
-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.. 希望这会有所帮助..

于 2013-05-24T05:28:30.377 回答