iOS JSON字符串转化为字典-字典转Json字符串-
1. JSON字符串转化为字典
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
if (jsonString == nil) {
return nil;
} NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if(err)
{
NSLog(@"json解析失败:%@",err);
return nil;
}
return dic;
}
2. 字典转Json字符串
// 字典转json字符串方法 -(NSString *)convertToJsonData:(NSDictionary *)dict { NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error]; NSString *jsonString; if (!jsonData) { NSLog(@"%@",error); }else{ jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; } NSMutableString *mutStr = [NSMutableString stringWithString:jsonString]; NSRange range = {,jsonString.length}; //去掉字符串中的空格 [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range]; NSRange range2 = {,mutStr.length}; //去掉字符串中的换行符 [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2]; return mutStr; }
iOS JSON字符串转化为字典-字典转Json字符串-的更多相关文章
- iOS开发--字典(NSDictionary)和JSON字符串(NSString)之间互转
iOS开发--字典(NSDictionary)和JSON字符串(NSString)之间互转 1. 字典转Json字符串 // 字典转json字符串方法 -(NSString *)convertToJs ...
- Python处理json字符串转化为字典
有一个需求,需要用python把json字符串转化为字典 inp_str = " {'k1':123, 'k2': '345','k3','ares'} " import json ...
- 使用eval将字符串转化成字典时报name 'null' is not defined错误解决办法
在接口测试过程中,为了取值将形如字典形式的字符串使用eval()方法转化成字典方便取值 str={"code":100,"num":1,"data&q ...
- 爬虫3 requests之json 把json数据转化为字典
#json 将json数据转化为字典,方便操作数据 res = requests.get('http://httpbin.org/get') print(res.json()) #res.json() ...
- json数据处理:读取文件中的json字符串,转为python字典
方法1: 读取文件中的json字符串, 再用json.loads转为python字典 import json str_file = './960x540/config.json' with open( ...
- python2.7字典转换成json时中文字符串变成unicode的问题:
参考:http://blog.csdn.net/u014431852/article/details/53058951 编码问题: python2.7字典转换成json时中文字符串变成unicode的 ...
- 字典,字符串,元组,字典,集合set,类的初步认识,深浅拷贝
Python之路[第二篇]:Python基础(一) 入门知识拾遗 一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. if 1==1: name = 'Jaso ...
- python字典保存至json文件
import os import json class SaveJson(object): def save_file(self, path, item): # 先将字典对象转化为可写入文本的字符串 ...
- Python基础(二) —— 字符串、列表、字典等常用操作
一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 二.三元运算 result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为 ...
随机推荐
- ubuntu16.04画图软件kolourpaint
1.安装kolourpaint sudo apt-get install kolourpaint4 -y 在里面搜索“kolourpaint”这个软件名.
- 开发板tftp:timeout问题
想要从PC上面tftp文件的时候遇到了tftp:timeout的问题: >: tftp -gr gprsapp 192.168.1.38tftp: timeout 检查了网络,可以ping的通P ...
- UE4 场景展示Demo
使用到的level blueprint如下:
- WP8简单的计算器
<Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinition ...
- JanusGraph与Cassandra集成模式
//如果使用的是cassandra 2.2或更高版本,需要开启thift,以使janus连接到cassandra. ./bin/nodetool enablethrift. 15.1 Local Se ...
- Centos7 卸载rpm包、卸载yum安装的包
1. 通过rpm -q <关键字>查到rpm包的名字.2. 调用rpm -e <包名>删除特定的rpm包
- Mysql 没有nvl()函数,却有一个类似功能的函数ifnull()
今天自己无聊写了看了一个查询需求随手写了一个sql语句,发现竟然不能运行,MySQL报[Err] 1305 - FUNCTION ceshi.nvl does not exist的错.才意识到自己写的 ...
- Atitit atiMail atiDns新特性 v2 q39
Atitit atiMail atiDns新特性 v2 q39 V1 实现了基础的功能 V2 重构..使用自然语言的方式 c.According_to_the_domain_name(&quo ...
- linux $* 和$@ if [ ](字符串比较)
$* 将命令后面的参数理解为一个类似为字符串,$@理解为多个单个的参数,类似理解成数据 $#参数总数 [root@mini0 test]# ./test4.sh jskd sj21 Using the ...
- UVa 12563 劲歌金曲 刘汝佳第二版例题9-5;
Problem J Jin Ge Jin Qu [h]ao (If you smiled when you see the title, this problem is for you ^_^) Fo ...