ios - 将 JSON 数据发布到特定对象 iOS

标签 ios objective-c xml json post

我已经在 Stack 上阅读了几篇关于如何使用 XML 和 JSON 发布数据的文章,但是关于如何更新选定的对象没有什么让我印象深刻的。

我正在从我老板的工作跟踪 API 中提取数据,一切正常。但是我想更新单个选定对象的变量。

例子:

If I have a recipe site API and I am pulling down 100 recipes and displaying the titles in a table. I select a row and it displays a new screen which shows all the details about the recipe with textfields for updating content and a button that will save to the website. How do I update an ingredient on the selected recipe so that it is represented on the website?

切记,我使用的是我公司的 API,而不是 Parse.com 或其他基于服务器的数据库。

我更愿意使用 JSON,但是如果有更好的使用 XML 的解决方案,我会接受。

新思想

更新时是否必须替换整个对象?

更多信息

由于使用 API,我没有直接指向特定对象的链接。我得到了 block (100 个食谱),必须遍历 block 以将每个对象保存为 NSObject,然后按照我喜欢的方式对新存储的 NSObject 进行排序。当我选择一行时,我选择了一个已排序的对象并显示该对象的内容。我不知道这些信息是否有帮助。

这个额外信息的原因是:

尝试通过 API 更新对象时,我如何知道我更新的是特定对象而不是整个对象列表?

最佳答案

我认为您想将数据发布到现有字段上:

你可以试试这个来发布数据:

 NSString  *post = [NSString stringWithFormat:@"text_field1=%@",text_field.text];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://your api to recieve the data into the fields"]]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    if(conn) {
        NSLog(@"Connection Successful");

    } else {
        NSLog(@"Connection could not be made");
    }
    }

关于ios - 将 JSON 数据发布到特定对象 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29640272/

相关文章:

ios - 具有不同对齐 subview 的 UIStackview

ios - Controller 之间的导航 - 黑屏

java - 设置 NetworkImageView 与 Volley 禁用图像的持有者

ios - 具有多个类的 Storyboard UIViewController

ios - UITableView 自定义单元格选项无法修改源变量

ios - 在 Swift 中使用 NSCoding 在分层类中进行数据持久化的最佳实践是什么?

ios - Soundcloud 标签音乐效果 Objective C

objective-c - 方法中间的可选 block : safe?

javascript - 检查 XML HTTP 请求中的重复值

python - 如何强制将所有 namespace 声明附加到根元素?