0

在我的应用程序中,我通过以下方式使用 JSON 上传数据:

NSMutableDictionary *titles = [NSMutableDictionary dictionary];

for (UITextField *textField in messagename)
{
    [titles setObject: textField.text forKey: @"title"];
    // as you can see, here you're replacing the value @ at key "title" with a new object on every pass
}

NSMutableDictionary *message = [NSMutableDictionary dictionary];
for (UITextField *textField in messagetext)
{
    [message setObject: textField.text forKey: @"title"];
    // as you can see, here you're replacing the value @ at key "title" with a new object on every pass
}

NSMutableDictionary *all = [NSMutableDictionary dictionary];

for (UITextField *textField in messagename)
{
    [all setObject: titles forKey: message];
    // as you can see, here you're replacing the value @ at key "title" with a new object on every pass
}

NSString *jsonString = [all JSONRepresentation];
NSData *jsonData = [jsonString dataUsingEncoding: NSUTF8StringEncoding];

[jsonData writeToFile: getImagePath atomically: YES];


NSString *destDir = @"/sandbox/";
[[self restClient] uploadFile:filename toPath:destDir
                withParentRev:nil fromPath:getImagePath];

唯一的问题是只上传了最后创建的表格的文本,而不是全部。我将标签与案例一起使用,但它太长了,如果一个对象为零,应用程序就会崩溃。另外,我只能用这个结果单独上传“消息”或“消息名称”:

{"title":"Untitledjjjj"}

“all”的全部目的是有类似的东西:message1(主键)带有下键标题,带有相应的标题,以及带有相应消息的消息。然后消息2...

我怎样才能做到这一点??如果我上传所有结果是 {}

4

1 回答 1

2

您一直在使用相同的键.. 可能您应该使用数组来代替titlesand message

我将修复第一部分,这应该足够了:

NSMutableArray *titles = [NSMutableArray array];

for (UITextField *textField in messagename)
{
    // add textfield contents to array
    [titles addObject: textField.text];
}

你明白吗?在字典中,一个键只能有一个值。所以你总是用每次调用来替换内容[dicitonary setObject: ... forKey: @"title"];

于 2012-05-11T16:09:03.650 回答