NSJSONSerialization from NSString

后端 未结 4 639
眼角桃花
眼角桃花 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:27

    You need to convert your NSString to NSData, at that point you can use the +[NSJSONSerialization JSONObjectWithData:options:error:] method.

    NSString * jsonString = YOUR_STRING;
    NSData * data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError * error = nil;
    id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    if (!json) {
        // handle error
    }
    

提交回复
热议问题