将键的值附加到同一个键上

时间:2016-12-15 13:41:54

标签: python json mongodb

这就是我目前所拥有的

coll = con['X']['Y']
s = "meta http equiv"

m = {'i': s}
n = json.dumps(m)
o = json.loads(n)
coll.insert(o)

数据

{ 
    "_id" : ObjectId("58527fe656c7a95cfaf40a15"), 
    "i" : "meta http equiv"
}

现在在下一次迭代中,s将会更改(根据我的计算),并且我想将s的值附加到相同的键

让我们说在下一次迭代中,sample test data and上的i成为{ "_id" : ObjectId("58527fe656c7a95cfaf40a15"), "i" : "meta http equiv sample test data and" }

所以我想要这个

//NSString *jsonRequest = [NSString stringWithFormat:@"{\"Email\":\"%@\",\"FirstName\":\"%@\"}",user,fname];
NSLog(@"Request: %@", jsonRequest);

NSURL *url = [NSURL URLWithString:@"http://golf.42point5.com/API/Jsonapi.php?method=singup"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

//NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
    NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:request
                                                       completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                          NSLog(@"Response:%@ %@\n", response, error);
                                                           if(error == nil)
                                                          {

                                                              NSString * text = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];
                                                                              NSLog(@"Data = %@",text);
                                                                          }

                                                      }];
    [dataTask resume];

如何实现这一目标?

1 个答案:

答案 0 :(得分:-1)

改变你的形成方式:

s = "meta http equiv"
s = (coll.get('i', '') + ' ' + s) if coll.get('i', '') else s

如果不是dict对象,请使用getattr代替:

s = "meta http equiv"
s = (getattr(coll, 'i', '') + ' ' + s) if getattr(coll, 'i', '') else s