0

在我的应用程序中,用户可以创建 UITextFields。向每个字段添加一个标签,以便标签对应于以下情况:1、2、3、4,...然后我将所有内容添加到 NSDictionary 和 json 表示中:

-(IBAction)buttonDropBoxuploadPressed:(id)sender{

//screenshot

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy_MM_dd"];


NSString *filename = [NSString stringWithFormat:@"By: %@  ", 
                      [formatter stringFromDate:[NSDate date]]];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                  [NSString stringWithFormat:@"%@", filename] ];

//[data writeToFile:path atomically:YES];


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

// [[self restClient] loadMetadata:@"/sandbox/"];

//JSON


NSString *object;   
NSString *object2;
NSString *object3;
NSString *object4;
NSString *object5;
NSString *object6;
NSString *object7;
NSString *object8;
NSString *object9;
NSString *object10;
NSString *object11;
NSString *object12;
NSString *object13;
NSString *object14;
NSString *object15;


for (UITextField *text in messagename) {


    int touchedtag = text.tag;

    NSUInteger tagCount = touchedtag;
    switch (tagCount) {

        case 1: 

            object = [NSString stringWithFormat:@"%@", text.text];

            break;
        case 2: 
            object2 = [NSString stringWithFormat:@"%@", text.text];

            break;
        case 3: 

            object3 = [NSString stringWithFormat:@"%@", text.text];

            break;
        case 4: 

            object4 = [NSString stringWithFormat:@" %@", text.text];

            break;

        case 5: 

            object5 = [NSString stringWithFormat:@"%@", text.text];

            break;


        case 6: 

            object6 = [NSString stringWithFormat:@"%@", text.text];

            break;

        case 7: 

            object7 = [NSString stringWithFormat:@"%@", text.text];

            break;
        case 8: 

            object8 = [NSString stringWithFormat:@"%@", text.text];

            break;
        case 9: 

            object9 = [NSString stringWithFormat:@"%@", text.text];

            break;
        case 10: 

            object10 = [NSString stringWithFormat:@"%@", text.text];

            break;
        case 11: 

            object11 = [NSString stringWithFormat:@"%@", text.text];

            break;
        case 12: 

            object12 = [NSString stringWithFormat:@"%@", text.text];

            break;
        case 13: 

            object13 = [NSString stringWithFormat:@"%@", text.text];

            break;
        case 14: 

            object14 = [NSString stringWithFormat:@"%@", text.text];

            break;
        case 15: 

            object15 = [NSString stringWithFormat:@"%@", text.text];

            break;


        default :

            break;



    }
}
//arrays

NSString * objects[] = { object, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11, object12, object13, object14, object15};

NSMutableArray *textnameobject = [[NSMutableArray alloc] initWithCapacity:b];

textnameobject = [NSMutableArray arrayWithObjects:objects count:b];



NSMutableArray *textnamekeys = [[NSMutableArray alloc] initWithCapacity:b];
NSString * textnumber[] = {@"title", @"title", @"title",@"title", @"title", @"title", @"title", @"title", @"title", @"title", @"title", @"title", @"title", @"title"};

textnamekeys = [NSMutableArray arrayWithObjects:textnumber count:b];


//arrays

NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObject: textnameobject forKey:textnamekeys];

/*

NSArray *objects2 = [NSArray arrayWithObjects:jsonDictionary, nil];
NSArray *keys2 = [NSArray arrayWithObjects:allkeys, nil];

NSDictionary *mainDict = [NSDictionary dictionaryWithObjects:objects2 forKeys:keys2];

 */

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

[jsonData writeToFile:path atomically:YES];

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

[[self restClient] loadMetadata:@"/sandbox/"];

//JSON    

}

当我按下按钮时,我收到以下错误:

JSON 表示失败。错误跟踪是:( "Error Domain=org.brautaset.JSON.ErrorDomain Code=1 \"JSON object key must be string\" UserInfo=0x2e8370 {NSLocalizedDescription=JSON object key must be string}" )

并因此导致保管箱错误。这适用于我以前的应用程序,代码完全相同。正确添加了 json 库。看不懂!!请帮忙!

4

1 回答 1

1

你的代码,重写。

- (IBAction)buttonDropBoxUploadPressed: (id)sender
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat: @"yyyy_MM_dd"];

    NSString *filename = [NSString stringWithFormat: @"By: %@ ", [formatter stringFromDate: [NSDate date]]];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex: 0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent: filename];

    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
    }

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

    [jsonData writeToFile: path atomically: YES];

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

但是,关于我的评论,您实际上并没有将文本字段的文本序列化为任何可用的内容。在此结束时,充其量,您将拥有如下所示的内容:

{
    "title": "My Text Field Value"
}

虽然我也相对确定您的一个或多个文本字段的 text 是nil,这会导致您的 JSON 问题。

于 2012-05-09T20:15:47.410 回答