ios - 将本地数据json转换为远程数据json

标签 ios objective-c json parsing

我有本地 sample_data.son 的工作代码,但我想使用远程 sample_data.json。它正在工作,但我没有找到真正的方法。我的代码如下。

- (void)generateData
{
    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Add code here to do background processing
        //
      //
        NSError* err = nil;
        data = [[NSMutableArray alloc] init];
        companyData = [[NSMutableArray alloc] init];
        NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"sample_data" ofType:@"json"];
        NSArray* contents = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath] options:kNilOptions error:&err];
        dispatch_async( dispatch_get_main_queue(), ^{
            // Add code here to update the UI/send notifications based on the
            // results of the background processing
            [contents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                [data addObject:[NSDictionary dictionaryWithObjectsAndKeys:[[obj objectForKey:@"first_name"] stringByAppendingString:[NSString stringWithFormat:@" %@", [obj objectForKey:@"last_name"]]], @"DisplayText", [obj objectForKey:@"email"], @"DisplaySubText",obj,@"CustomObject", nil]];
                [companyData addObject:[NSDictionary dictionaryWithObjectsAndKeys:[obj objectForKey:@"company_name"], @"DisplayText", [obj objectForKey:@"address"], @"DisplaySubText",obj,@"CustomObject", nil]];
            }];
        });
    });
}

#pragma mark MPGTextField Delegate Methods

- (NSArray *)dataForPopoverInTextField:(MPGTextField *)textField
{
    if ([textField isEqual:self.name]) {
        return data;
    }
    else if ([textField isEqual:self.companyName]){
        return companyData;
    }
    else{
        return nil;
    }
}

- (BOOL)textFieldShouldSelect:(MPGTextField *)textField
{
    return YES;
}

- (void)textField:(MPGTextField *)textField didEndEditingWithSelection:(NSDictionary *)result
{
    //A selection was made - either by the user or by the textfield. Check if its a selection from the data provided or a NEW entry.
    if ([[result objectForKey:@"CustomObject"] isKindOfClass:[NSString class]] && [[result objectForKey:@"CustomObject"] isEqualToString:@"NEW"]) {
        //New Entry
        [self.nameStatus setHidden:NO];
    }
    else{
        //Selection from provided data
        if ([textField isEqual:self.name]) {
            [self.nameStatus setHidden:YES];
            [self.web setText:[[result objectForKey:@"CustomObject"] objectForKey:@"web"]];
            [self.email setText:[[result objectForKey:@"CustomObject"] objectForKey:@"email"]];
            [self.phone1 setText:[[result objectForKey:@"CustomObject"] objectForKey:@"phone1"]];
            [self.phone2 setText:[[result objectForKey:@"CustomObject"] objectForKey:@"phone2"]];
        }
        [self.address setText:[[result objectForKey:@"CustomObject"] objectForKey:@"address"]];
        [self.state setText:[[result objectForKey:@"CustomObject"] objectForKey:@"state"]];
        [self.zip setText:[[result objectForKey:@"CustomObject"] objectForKey:@"zip"]];
        [self.companyName setText:[[result objectForKey:@"CustomObject"] objectForKey:@"company_name"]];
    }
}

只需要将本地解析代码改为远程解析代码即可。

您还可以在这里看到带有本地 json 的原始项目 https://github.com/gaurvw/MPGTextField

最佳答案

不是答案,而是一个更清晰的代码示例,作为格式化的答案。

原文:

[data addObject:[NSDictionary dictionaryWithObjectsAndKeys:[[obj objectForKey:@"first_name"] stringByAppendingString:[NSString stringWithFormat:@" %@", [obj objectForKey:@"last_name"]]], @"DisplayText", [obj objectForKey:@"email"], @"DisplaySubText",obj,@"CustomObject", nil]];

重新格式化:

NSString *fullName = [NSString stringWithFormat:@"%@ %@", obj[@"first_name"], obj[@"last_name"]];
[data addObject: @{
                   @"DisplayText"    : fullName,
                   @"DisplaySubText" : obj[@"email"],
                   @"CustomObject"   : obj
                   }];

关于ios - 将本地数据json转换为远程数据json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33723047/

相关文章:

javascript - Safari 浏览器 audio.play() 不工作

objective-c - 向 NSMutableDictionary 添加条目

javascript - JSON 从 python json.dumps 到 javascript JSON.parse() 并转义\"

json - 如何从 package.json 加载 Angular (v4+)应用程序中的版本号?

python - 如何使用python每次生成随机json数据?

ios - 取消 NSOperation 完成 block

ios - iOS 上的 native 代码部署

ios - 快速添加谷歌日历事件

ios - 如何让另一个 AppStore 应用程序打开一个预填充的原生消息应用程序?

ios - UITableView - 使用 reloadRowsAtIndexPath