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";
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM