我有以下格式的 JSON 数据,我如何将值发布到其中,因为它给了我错误:域:NSCocoaErrorDomain”Json 文本没有以数组或对象开头,并且没有设置允许片段的选项!!!请帮助!!!
[
{ "No:"1" , "firstname":dhruv ....},
{ "No:"2", "firstname" : something i want to post...} ]
代码:
Nsstring* URL string = [NStringwithformat:@Myurl?fname=%(value from editext ]
NSArray *arrData = @[@{
@"No": @"1",
@"firstname": @"dhruv"},
@{
@"No": @"2",
@"firstname": @"something i want to post"
}];
NSData *data = [NSJSONSerialization dataWithJSONObject:arrData options:0 error:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"your url here"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *dataTask = [session uploadTaskWithRequest: request
fromData:data completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//Kindly use below either one according to your response
//If the response is in Dictionary format
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", jsonDict);
//If the response is in Array format
NSArray *jsonArr = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", jsonArr);
}];
[dataTask resume];
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句