1

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?

AmineG
  • 1,908
  • 2
  • 27
  • 43

1 Answers1

3

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];
pbx
  • 1,137
  • 7
  • 15