如何将此NSString转换为NSDictionary?下面是的NSString:需要一些帮助将JSON字符串转换为NSDictionary
NSLog(@"urlstring: %@", urlString);
输出:urlstring: "{"url":"http://mydomain.com/metaioman_target.png"}"
然后我把它转换成JSON,然后到的NSDictionary:
NSData *json = [urlString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:json options:0 error:&e];
然而,当我NSLog
的dic
为null,印刷该错误给出如下:
error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)"
(JSON text did not start with array or object and option to allow fragments not set.)
UserInfo=0x16641de0 {NSDebugDescription=JSON text did not start with
array or object and option to allow fragments not set.}
然而,覆盖urlString
有:
@"{\"ID\":\"Content\":268,\"type\":\"text\"},\"ContractTemplateID\":\"Content\":65,\"type\":\"text\"}}"
成功地将其转换。
我猜猜报价有问题吗?
的'NSLog'应该输出=>'urlstring:{ “URL”:“HTTP:/ /mydomain.com/metaioman_target.png“}',所以你的问题是外双引号,摆脱他们,它会反序列化就好。 – Alladinian
@Alladinian是对的。删除外部双引号后尝试转换。 – NightFury