博客> 字符串的转码截取,将部分内容转换成字典
字符串的转码截取,将部分内容转换成字典
2018-12-12 05:02 评论:0 阅读:184 suifumin
后台返回数据处理

后台返回的数据格式 alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2018032602447356&biz_content={"body":"AP433654871174938624","out_trade_no":"AP433654871174938624","product_code":"QUICK_MSECURITY_PAY","subject":"支付订单","timeout_express":"30m","total_amount":"9.36"}&charset=UTF-8&format=json&method=alipay.trade.app.pay&notify_url=http://47.104.178.18/yj-api/callback/alipayCallback&sign=iS5xTxsbpcjIfpyfEVl+vDYTzvyzl50pgco2j20EVxyWyOJ/e1EcV0U5TrKzevn4sF2scns0AdB+m8s0lHjsBLNk9n3E675G7Z3YVH954q/tw454dMeT4EMrqUYl5dAef78eoSAq2PO5+T2kuhVIk4ywR4loGUf7Baz2wk+to3Jnhsc7lxcLhEMf6kn8ULMA+b7TB7fQTx0D32vSzVjwR2cssRFeMSE2lTLynH3LKQC6adVbD7eEXu8n+/SqyCypnaAIfDQtUA1qZNhyLjCUeRbJ2LZPiIAYSf07//UtcZt0iimKiVCNZOG6GghNI30VB1KGnAG+BDlUqSo9xpi+Xw==&sign_type=RSA2×tamp=2018-04-11+15:49:38&version=1.0" 最终想要的结果(目的要biz_content的内容) alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2018032602447356&biz_content={"body":"AP433654871174938624","out_trade_no":"AP433654871174938624","product_code":"QUICK_MSECURITY_PAY","subject":"支付订单","timeout_express":"30m","total_amount":"9.36"}&charset=UTF-8&format=json&method=alipay.trade.app.pay&notify_url=http://47.104.178.18/yj-api/callback/alipayCallback&sign=iS5xTxsbpcjIfpyfEVl+vDYTzvyzl50pgco2j20EVxyWyOJ/e1EcV0U5TrKzevn4sF2scns0AdB+m8s0lHjsBLNk9n3E675G7Z3YVH954q/tw454dMeT4EMrqUYl5dAef78eoSAq2PO5+T2kuhVIk4ywR4loGUf7Baz2wk+to3Jnhsc7lxcLhEMf6kn8ULMA+b7TB7fQTx0D32vSzVjwR2cssRFeMSE2lTLynH3LKQC6adVbD7eEXu8n+/SqyCypnaAIfDQtUA1qZNhyLjCUeRbJ2LZPiIAYSf07//UtcZt0iimKiVCNZOG6GghNI30VB1KGnAG+BDlUqSo9xpi+Xw==&sign_type=RSA2×tamp=2018-04-11 15:49:38&version=1.0

解决办法: 1.首先将其转码 NSString decodedString=(__bridge_transfer NSString )CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)string, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)); 2.截取 NSRange startRange = [decodedString rangeOfString:@"biz_content="]; NSRange endRange = [decodedString rangeOfString:@"&charset="]; NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length); NSString result = [decodedString substringWithRange:range]; NSLog(@"%@",result); NSData jsonData = [result dataUsingEncoding:NSUTF8StringEncoding]; NSError error; NSDictionary dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; 这个地点就是要得到的内容

收藏
0
sina weixin mail 回到顶部