参数问题的JSON映射与' \'

时间:2017-05-22 07:30:32

标签: objective-c json post mapping nsdictionary

来自(application/json) API邮递员的预期原始数据POST如下:

{"survey_answer":{"answers_json": "{\"4090442c-90ce-42c2-aae8-7c812b7c0f04\":\"test from postman\"}"}}

如何概括要发送给API请求的参数? 它是字典吗?如果是这样我如何初始化它?4090442c-90ce-42c2-aae8-7c812b7c0f04是调查ID,取决于正在回答的问题和"测试邮递员"是通过textfield输入的答案。

谢谢! @Larme我正在调查AnsAnswerForCurrentSurvey作为字符串 我更新了以下代码来更新密钥" f884a7d1-f9d9-4563-bb6e-94538664f3bd" : -

NSData *data = [surveyAnswerForCurrentSurvey dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *answerJson = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSMutableDictionary *answer = [answerJson mutableCopy];
if([answer valueForKey:question.name] != nil){
    [answer setObject:textField.text forKey:question.name];
}
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[answer copy] options:0 error:nil];
NSLog(@"ns data is %@",jsonData);
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"json string:%@", json);
NSDictionary *answersDict = @{@"answers_json":json};
NSData *answersAsData = [NSJSONSerialization dataWithJSONObject: answersDict options:0 error:nil];
NSString *answersJSONStringify = [[NSString alloc] initWithData: answersAsData encoding:NSUTF8StringEncoding];
parameters = @{@"survey_answer":answersJSONStringify};

但它给我的结果是 {     " survey_answer" =" {\" answers_json \":\" {\\" 7d2c591c-9056-405c-9509-03266842b7e5 \\":[\\ && #34; 1 \\&#34],\\" 54bdcf13-e500-418a-8bab-d0639e7e1e28 \\":\\" 2 \\",\\ " 63bb0722-7099-4820-a400-36b8938c6ae8 \\":\\"你好\\",\\" f884a7d1-f9d9-4563-bb6e-94538664f3bd \ \":\\"测试来自cms \\",\\" ed3acc20-4ae4-493e-ac55-4d2d0f282886 \\":\\" 1 \\"} \"}&#34 ;; }

所以我得到' \\'因此,在最终结果中,请求参数的格式不正确。如何避免它们?

0 个答案:

没有答案