以 x-www-form-urlencoded 的形式发送字典中的数据,但需要 json

问题描述 投票:0回答:1

我以 url 编码形式发送数据

JobId=564876565946&UstaId=242816809697&Comment=&Ratings=%5B%7B%0A%20%20%20%20QuestionId%20%3D%203533%3B%0A%20%20%20%20Rating%20%3D%204%3B%0A%7D%2C%7B%0A%20%20%20%20QuestionId%20%3D%209767%3B%0A%20%20%20%20Rating%20%3D%205%3B%0A%7D%5D

当我解码这些数据时

JobId=564876565946&UstaId=242816809697&Comment=&Ratings=[{
    QuestionId = 3533;
    Rating = 4;
},{
    QuestionId = 9767;
    Rating = 5;
}]

如上图所示 但在邮递员中,当我发送此表单时,它成功了

{
    "JobId": 564876565946,
    "UstaId": 242816809697,
    "Ratings": [
        {
            "QuestionId": 3533,
            "Rating": 4
        },
        {
            "QuestionId": 9767,
            "Rating": 4
        }
    ],
    "Comment": "test postman"
}

json格式

我创建的参数是这样创建的

       NSDictionary *params = @{
                                @"Ratings":[NSString stringWithFormat:@"[%@]",[self.ratings componentsJoinedByString:@","]],
                                @"Comment":comment,
                                @"UstaId":[self.ustaID stringValue],
                                @"JobId":[self.jobID stringValue]
        };

我将我的答案添加到 self. ratings(这是一个 NSMutableArray),如下所示

  for(RatingQuestion * question in AppDlg.reviewQustions){
        self.answer=[[NSMutableDictionary alloc]init];
        [self.answer setObject:[question.ID stringValue]forKey:@"QuestionId"];
        [self.answer setObject:@5 forKey:@"Rating"];
        [self.ratings addObject:self.answer];
    }

代码是用针对 ios 的 Objective-C 编写的,我可以根据要求提供更多信息

我希望正确发送我的 url 编码数据

ios json objective-c api
1个回答
0
投票

首先,我建议在构建评级数组时不要使用

stringValue
。您想要的数字为
NSNumber
:

self.ratings = [[NSMutableArray alloc] init];
for (RatingQuestion *question in reviewQuestions) {
    [self.ratings addObject:@{
        @"QuestionId": question.ID,
        @"Rating": question.rating,
    }];
}

然后,您想要构建 JSON,只需将您的

ratings
NSArray
NSDictionary
)放入
params
字典中(并且再次避免使用
stringValue
ustaID
中的
jobID
):

NSDictionary *params = @{
    @"Ratings": self.ratings,
    @"Comment": self.comment,
    @"UstaId": self.ustaID,
    @"JobId": self.jobID
};

然后,要构建 JSON,请使用

NSJSONSerialization
:

NSError *error = nil;
NSData *json = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:&error];
if (!json) {
    NSLog(@"error: %@", error);
    return;
}

现在,您可以随心所欲地使用它

json
。例如,您可能想要设置
HTTPBody
NSMutableURLRequest

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
request.HTTPMethod = @"POST";
request.HTTPBody = json;

如果您想确认 JSON 是否符合您的预期(用于调试目的),您还可以创建该

NSString
NSData

NSString *string = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);

将输出:

{
  "JobId" : 564876565946,
  "UstaId" : 242816809697,
  "Comment" : "test postman",
  "Ratings" : [
    {
      "QuestionId" : 3533,
      "Rating" : 4
    },
    {
      "QuestionId" : 9767,
      "Rating" : 4
    }
  ]
}

注意,上面我使用了

NSJSONWritingPrettyPrinted
选项。一旦您确认 JSON 是您想要的,您就可以使用
0
kNilOptions
来代替。但是,在目视检查 JSON 的正确性时,漂亮的打印格式非常有用。


注意,我们确保

NSDictionary
的数值为
NSNumber
而不是
NSString
的原因是
NSJSONSerialization
会在字符串周围加上引号,而不是在数值周围。您的 Postman 示例的数值周围没有引号,因此我假设您也不希望 Objective-C 代码引入引号。

© www.soinside.com 2019 - 2024. All rights reserved.