0

I have a NSString *str = @"abcd", and I want to serialize it as JSON String which becomes "abcd". How should I do?

I have tried this NSJSONSerialization from NSString, but it doesn't work. The returned id is 0x0.

This is my tricky code what I am using now to do this

NSDictionary *dict = @{@"result": str};
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
NSString *body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
body = [body substringFromIndex:14];
body = [body substringToIndex:body.length - 2];

Finally I read spec about JSON and know that OC follows old JSON spec that just thinks object and array is valid. That's the reason why NSJSONSerialization cannot serialize JSON string.

zyf0330
  • 41
  • 4
  • Define "doesn't work." – NRitH May 24 '19 at 08:52
  • Read the error. It's not a really valid "JSON" (well except it you use .allowFragments in the decoding). In fact, if you just want to pass it, just do `NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding]`? – Larme May 24 '19 at 10:15
  • Please add more code. – Roman Podymov May 24 '19 at 10:24
  • I have no more code, I just want to find a method to serialize OC string as json string. JSON contains six types within String, why this string is not a valid string for JSON? – zyf0330 May 26 '19 at 02:21
  • Your code makes no sense. A single JSON string is just a string so you can write `NSString *body = str;` – vadian May 27 '19 at 04:40
  • Your code on json serialization is correct. I think the issue is the substringFromIndex: is problematic. Try to comment out the 2 last lines and NSLog on body after initWithData. – GeneCode May 27 '19 at 05:20
  • Do you really just want to JSON-encode a string, so that special characters are escaped and so forth? – Caleb May 27 '19 at 05:22
  • @vadian Nonono, a JSON String is useful to do data exchange cross different language. – zyf0330 May 28 '19 at 05:20
  • @GeneCode `options:NSJSONWritingPrettyPrinted` adds extra space chars, so I must remove them by `substring`. That is not the focus point. – zyf0330 May 28 '19 at 05:22
  • @Caleb Yeah! I just want to JSON-encode a string. The way I using now is not a simple direct method. It is a trick. – zyf0330 May 28 '19 at 05:23
  • The result of your code is exactly the same as `NSString *body = str`. It's unclear what you are going to accomplish. Anyway it's pretty pointless to create a dictionary, convert it to JSON and extract the dictionary value with string manipulation. There is no difference between a plain string and the JSON representation of a plain string. – vadian May 28 '19 at 06:43
  • @vadian If a string literal is `abcd`,then its JSON-encoded is `"abcd"`. If a string literal is `"abcd"`, then its JSON-encoded is `"\"abcd\""`. And, you should distinguish string as NSString in OC runtime and as JSON for data exchange in network. They don't equal. – zyf0330 May 29 '19 at 08:52
  • OK, then just add the double quotes and write `NSString *body = [NSString stringWithFormat:@"\"%@\"", str];` – vadian May 29 '19 at 08:57
  • @vadian I just want a common api to do this, not constructing it by myself. It is so strange I cannot find it, it should be a standard api. – zyf0330 May 29 '19 at 09:37
  • @zhuisui That's a special case for JSON. "Quite new". iOS allows it in the decoding using `NSJSONReadingAllowFragments`, but there is no option in writing, since I guess it's just easily done with doing as said by vadian. – Larme May 31 '19 at 13:52
  • @LarmeThanks, but what I need is not read. Adding double quote around string is not enough, you must handle escape. I know a third-party JSON lib which supports serializing JSON string, so maybe can use it. – zyf0330 Jun 01 '19 at 05:43

0 Answers0