将本地数据 json 更改为远程数据 json

Posted

技术标签:

【中文标题】将本地数据 json 更改为远程数据 json【英文标题】:Change local data json to remote data json 【发布时间】:2015-11-15 18:01:59 【问题描述】:

我有本地 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:开头的语句是278个字符,比较难理解。首先是不必要的代码,数组和字典有一种文字语法,简化了它们的创建,最后一些东西,比如名字和姓氏的组合是中间语句的一个很好的候选者。编写代码应该是为了便于下一个开发人员而不是编译器理解。 有什么想法吗?还在等 【参考方案1】:

不是一个答案,一个更简洁的代码作为格式化答案的例子。

原文:

[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
                   ];

【讨论】:

以上是关于将本地数据 json 更改为远程数据 json的主要内容,如果未能解决你的问题,请参考以下文章

告诉Java代码,无论应用是在远程主机还是本地主机中运行

将远程 api json 数据显示到谷歌地图中

将远程 JSON 结果写入本地文件

跨域,但服务器数据不是 JSON 格式 [关闭]

如何从本地 JSON 文件将数据加载到 ViewController [关闭]

Kendo UI - JSON 响应 - 使用带有服务器分组和服务器聚合的远程数据源的网格