[英]How do I urlencode this NSString that contains a JSON inside it?
我有以下應發送到后端的json字符串
{
id = "MU_200255802";
keywords = (
Talk,
games,
meetup,
time,
meet,
"Time for Another Game"
);
}
因此,在此JSON之前,我有Java Servlet URL,例如
http://....net/servletName?
我應該如何對json字符串和url進行urlencode,因為即使嘗試了多個選項,我也會在委托方法中不斷收到錯誤的url作為錯誤。 什么是正確的方法/
我嘗試使用編碼
NSString *urlStringEncoded = [[NSString stringWithString:urlString] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
並且還使用了其他編碼格式。
用它來創建json字符串
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@"Your object" options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
您可以使用Google Toolbox for Mac
,在此處進行https://code.google.com/p/google-toolbox-for-mac/source/checkout進行檢出
有名為GTMNSString+HTML
, GTMNSString+XML
, GTMNSString+URLArguments
,其中包含許多可用於您的編碼方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.