URL 在 Objective-C 中解码,将 %7B 转换为 { 并将 %22 转换为 "
URL Decode in Objective-C, converting %7B to { and %22 to "
如何使用 Objective C 和 Xcode.
将 %7B
转换为 {
并将 %22
转换为 "
我的 jsonString 格式是:%7B%22maxTemp%22:%2235%22
看起来您需要 URL 解码该字符串 - 也许 following post 可能有用。
我认为你的 NSJSONSerialization
有问题。
这是将响应数据转换为 JSONObject
的示例:
[NSJSONSerialization JSONObjectWithData:[YourResponseData]
options:NSJSONReadingAllowFragments
error:&error];
关于你的问题,使用 stringByReplacingPercentEscapesUsingEncoding
即可:
NSString *string = [@"%7B%22maxTemp%22:%2235%22" stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(@"%@", string);
来自苹果documentation:
Return Value A new string made by replacing in the receiver all
percent escapes with the matching characters as determined by the
given encoding encoding. Returns nil if the transformation is not
possible, for example, the percent escapes give a byte sequence not
legal in encoding.
此外,请注意此函数在 iOS 9.0 中已弃用,您可能需要阅读相关内容。
输出:
{"maxTemp":"35"
如何使用 Objective C 和 Xcode.
将%7B
转换为 {
并将 %22
转换为 "
我的 jsonString 格式是:%7B%22maxTemp%22:%2235%22
看起来您需要 URL 解码该字符串 - 也许 following post 可能有用。
我认为你的 NSJSONSerialization
有问题。
这是将响应数据转换为 JSONObject
的示例:
[NSJSONSerialization JSONObjectWithData:[YourResponseData]
options:NSJSONReadingAllowFragments
error:&error];
关于你的问题,使用 stringByReplacingPercentEscapesUsingEncoding
即可:
NSString *string = [@"%7B%22maxTemp%22:%2235%22" stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(@"%@", string);
来自苹果documentation:
Return Value A new string made by replacing in the receiver all percent escapes with the matching characters as determined by the given encoding encoding. Returns nil if the transformation is not possible, for example, the percent escapes give a byte sequence not legal in encoding.
此外,请注意此函数在 iOS 9.0 中已弃用,您可能需要阅读相关内容。
输出:
{"maxTemp":"35"