NSJSONSerialization from NSString

后端 未结 4 647
眼角桃花
眼角桃花 2020-12-03 00:41

Is it possible if I have a NSString and I want to use NSJSONSerialization? How do I do this?

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 01:18

    You can convert your string to NSData by saying:

    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    

    You can then use it with NSJSONSerialization. Note however that NSJSONSerialization is iOS5 only, so you might be better off using a library like TouchJSON or JSONKit, both of which let you work directly with strings anyway, saving you the step of converting to NSData.

提交回复
热议问题