objective-c - iOS json数据打印解析
黄舟
黄舟 2017-04-17 16:53:08
0
4
819
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(4)
刘奇
  1. This is indeed not binary, it is unicode encoding. You can use the p command in lldb or the echo command in the shell to print these encodings, both of which can be printed out in a readable format. of Chinese. p 命令或在 shell 中使用 echo 命令打印这些编码,均可打印出可读的中文。

  2. 从 iOS 5 开始(没有验证,但早期打印数组和字典均和现在打印对象的显示一致,<名称:内存地址>),打印数组和字典显示出具体的数据结构(description 方法),更方便了调试,但其中打印字符串时并未做任何处理(如此处的字符转义)。由于只是便于调试,而且打印具体字符串时,调用字符串的 desciption 方法时,会转义为可读中文,也就是到具体处理的时候会自动转义,不用也不应该自己去转。

  3. 非得在控制台显示中文的话可以这样,NSLog(@"%@",[NSString stringWithCString:[[dictionary description] cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding]);;也可以重写 description 方法。但个人感觉其实没有这个必要。

  4. 补充:最近在使用 p 命令时,发现 p "U4e3dU5c71" 这种形式打印会报错 error: incomplete universal character name ,发现 u 的大小写是不一样,应该的形式是 p "u4e3du5c71" (四位)或者 p "U00004e3dU00005c71"(八位)。虽然没找到明确文字说明,但可以推论出,小写的是 BMK

    Starting from iOS 5 (no verification, but the early printed arrays and dictionaries are consistent with the current printed object display, <name: memory address>), printed arrays and dictionaries display specific data structures (description method), which is more convenient for debugging, but no processing is done when printing the string (such as character escaping here). Because it is only for debugging, and when printing a specific string, when calling the desciption method of the string, it will be escaped into readable Chinese, that is, it will be automatically escaped when it is processed specifically, and it is not necessary. You should change it yourself.
🎜🎜If you have to display Chinese on the console, you can do this, NSLog(@"%@",[NSString stringWithCString:[[dictionary description] cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding]);; you can also repeat Write the description method. But I personally feel that this is not necessary. 🎜🎜 🎜🎜Supplement: Recently when using the p command, I found that p "U4e3dU5c71" will report an error error: incomplete universal character name when printing in this form. I found that the capitalization of u is different. The correct form is p "u4e3du5c71" (four digits) or p "U00004e3dU00005c71" (eight digits). ). Although no clear text explanation was found, it can be inferred that the lower case is BMK🎜[NSString and Unicode]🎜, which defaults to plane 0 and the first four digits are 0 by default. 🎜🎜 🎜
刘奇

Convert json to dictionary and get data based on key value

左手右手慢动作

An iOS development novice answered angrily.
First of all, let me correct you, "u8bc4u8bbau6807u9898"is Unicode encoding, not "binary" as the questioner mentioned.
So just decode Unicode and convert it into Chinese~ There are probably two ways to solve it This question~

  1. Use an online conversion tool. There are a lot of searches on Baidu so I won’t give specific examples;

  2. Write a tool class for conversion and paste the code below;

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *unicodeString = @"\u8bc4\u8bba\u6807\u9898";
    NSLog(@"%@", [self replaceUnicode:unicodeString]);
}

// 将 Unicode 编码转换成中文
- (NSString *)replaceUnicode:(NSString *)unicodeStr {
    NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\u" withString:@"\U"];
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\""];
    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
                                                           mutabilityOption:NSPropertyListImmutable
                                                                     format:NULL
                                                           errorDescription:NULL];

    return [returnStr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"\n"]; 
}
左手右手慢动作

Post directly in lldb

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!