iOS:为以下JSON将字符串数组发布到web服务 如何从“post”Web服务获得以下响应
我有一个名为textary的字符串数组。 如何在下面的响应中为key@“Answer”发布文本数组iOS:为以下JSON将字符串数组发布到web服务 如何从“post”Web服务获得以下响应,ios,json,Ios,Json,我有一个名为textary的字符串数组。 如何在下面的响应中为key@“Answer”发布文本数组 { "ProjID": "78", "Uid": "12", "EmailID": "ratnam_nv@yahoo.com", "ProjectInviterFQAnswers": [{ "slno": "1", "Answer": "a1", "o
{
"ProjID": "78",
"Uid": "12",
"EmailID": "ratnam_nv@yahoo.com",
"ProjectInviterFQAnswers": [{
"slno": "1",
"Answer": "a1",
"order": "1",
"flag": "F"
}, {
"slno": "2",
"Answer": "a1",
"order": "2",
"flag": "F"
}, {
"slno": "1",
"Answer": "a1",
"order": "2",
"flag": "Q"
}
]
};
这是我到目前为止试过的
NSError *error = Nil;
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSDictionary *dictionaryArray1 = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"slno", @"a1", @"Answer", @"1", @"order", @"F", @"Flag", nil];
NSDictionary *dictionaryArray2 = [NSDictionary dictionaryWithObjectsAndKeys:@"2", @"slno", @"a1", @"Answer", @"1", @"order", @"F", @"Flag", nil];
NSDictionary *dictionaryArray3 = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"slno", @"a1", @"Answer", @"2", @"order", @"Q", @"Flag", nil];
NSArray *arrayAnswer = [NSArray arrayWithObjects:dictionaryArray1, dictionaryArray2, dictionaryArray3, nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"78", @"ProjID", @"12", @"UID", @"ratnam_nv@yahoo.com", @"EmailID", arrayAnswer, @"ProjectInviterFQAnswer", nil];
NSURL *url = [NSURL URLWithString:@" someurl "];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:&error];
[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 alloc] initWithRequest:request delegate:self];
[connection start];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if(error || !data)
{
NSLog(@"JSON Data not posted!");
[activity stopAnimating];
UIAlertView *alertMessage = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Data not saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertMessage show];
}
else
{
[activity startAnimating];
NSLog(@"JSON data posted! :)");
NSError *error = Nil;
NSJSONSerialization *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSLog(@"Response is %@", jsonObject); //Not getting the response here Message = "An error has occurred."
[activity stopAnimating];
}
}];
首先,您必须将响应转换为dictionary,并设置response您的textArray字符串的值,然后再次将其转换为json字符串。首先,您必须将响应转换为dictionary,并设置response您的textArray字符串的值,然后再次将其转换为json字符串小心:
NSArray *keys = [NSArray arrayWithObjects:@"slno", @"Answer", @"order", @"flage", nil];
但你有:
"flag": "Q"
鞭毛!=旗帜
这就是为什么使用常量很好
接下来,关于您的问题:
首先,您必须将JSON转换为NSDictionary
之后:
NSArray * array = [jsonDict valueForKey:@"ProjectInviterFQAnswer"];
for(NSMutableDictionary * dict in array){
[dict setValue:<put here anything you want> forKey:@""];
}
NSArray*数组=[jsonDict valueForKey:@“projectInviterQanswer”];
for(NSMutableDictionary*数组中的dict){
[dict setValue:forKey:@”“;
}
完成后,从jsonDict创建一个新的JSON,就完成了。请小心:
NSArray *keys = [NSArray arrayWithObjects:@"slno", @"Answer", @"order", @"flage", nil];
但你有:
"flag": "Q"
鞭毛!=旗帜
这就是为什么使用常量很好
接下来,关于您的问题:
首先,您必须将JSON转换为NSDictionary
之后:
NSArray * array = [jsonDict valueForKey:@"ProjectInviterFQAnswer"];
for(NSMutableDictionary * dict in array){
[dict setValue:<put here anything you want> forKey:@""];
}
NSArray*数组=[jsonDict valueForKey:@“projectInviterQanswer”];
for(NSMutableDictionary*数组中的dict){
[dict setValue:forKey:@”“;
}
完成后,从jsonDict创建一个新的JSON,就完成了。您尝试了什么?你面临什么问题?若您自己并没有尝试过任何东西,只不过是第一次尝试,但遇到了更具体的问题。请使用NSDictionary或NSMutableDictionary创建键/值对。祝你好运@CRDave请查看我的编辑您使用JSON的代码在哪里?你说的邮寄是什么意思?你想在服务器上发送JSON吗(因为这是发布的一般含义)?你尝试过什么?你面临什么问题?若您自己并没有尝试过任何东西,只不过是第一次尝试,但遇到了更具体的问题。请使用NSDictionary或NSMutableDictionary创建键/值对。祝你好运@CRDave请查看我的编辑您使用JSON的代码在哪里?你说的邮寄是什么意思?您想在服务器上发送JSON吗(因为这是发布的一般含义)?