NSString转化为NSDictionary
Posted apologize的ios学习足迹
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NSString转化为NSDictionary相关的知识,希望对你有一定的参考价值。
如有一段json格式的字符串
NSString *jsonStr = @"{ "api": "ccf.cart2.queryPointsBalance", "code": "1", "v": "1.0", "msg": "", "data": { "totalBalanceQuantity": "4", "totalBalanceAmt": "0.04", "errorInfos": { "errorCode": "", "errorMessage": "" } } }";
直接转换是有问题的,必须先转义(可以在json网站上转)。写成
NSString *jsonStr = @"{\"api\":\"ccf.cart2.queryPointsBalance\",\"code\":\"1\",\"v\":\"1.0\",\"msg\":\"\",\"data\":{\"totalBalanceQuantity\":\"4\",\"totalBalanceAmt\":\"0.04\",\"errorInfos\":{\"errorCode\":\"\",\"errorMessage\":\"\"}}}"; NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *tempDictQueryDiamond = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
然后打印tempDictQueryDiamond时为
{ api = "ccf.cart2.queryPointsBalance"; code = 1; data = { errorInfos = { errorCode = ""; errorMessage = ""; }; totalBalanceAmt = "0.04"; totalBalanceQuantity = 4; }; msg = ""; v = "1.0"; }
以上是关于NSString转化为NSDictionary的主要内容,如果未能解决你的问题,请参考以下文章
如何将 NSString 从 JSON 转换为 NSDictionary? [关闭]
将 Plist (NSString) 解析为 NSDictionary
如何将 NSString 转换为 NSDictionary?