json_encode等同于objective-c [英] json_encode equivalent for objective-c

查看:102
本文介绍了json_encode等同于objective-c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写相当于这个php代码

  json_encode(utf8_encode())

$ b



  [NSString stringWithCString:[testString cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSASCIIStringEncoding] 
/ pre>

所以现在,我需要实现相当于json_encode php函数。



input:http ://www.mydomain.com/s?c =Théâtre,Cinéma



预期输出:http://www.mydomain.com/s?c=有没有人有这样做的有效方式?

解决方案

你可以看看

  +(NSData *)dataWithJSONObject:(id)obj选项:(NSJSONWritingOptions)选择错误:(NSError **)错误

你从苹果基金会课程的某些对象中获取JSON数据。如果你需要一个字符串,你可以使用:

   - (id)initWithData:(NSData *)数据编码:(NSStringEncoding)编码NSString的

。可能导致以下情况:

  NSString * string = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:YOUR_OBJECT_HERE options:0 error:nil] encoding:NSUTF8StringEncoding] autorelease]; 


I need to write the equivalent to this php code

json_encode(utf8_encode())

in objective-c

I've implemented the equivalent to utf8_encode with

[NSString stringWithCString:[testString cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSASCIIStringEncoding]

So now, I need to implement the equivalent to json_encode php function.

input : "http://www.mydomain.com/s?c=Théâtre,Cinéma"

expected output :  "http://www.mydomain.com/s?c=Th\u00c3\u00a9\u00c3\u00a2tre,Cin\u00c3\u00a9ma"

Does anyone have an efficient way to do that?

解决方案

You could have a look at

+ (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error

You get back JSON data from some object of Apples Foundation classes. If you need a string, you could use:

- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding

of NSString. Might result in something like:

NSString *string = [[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:YOUR_OBJECT_HERE options:0 error:nil] encoding:NSUTF8StringEncoding] autorelease];

这篇关于json_encode等同于objective-c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆