- (void)json2Plist
{
NSString *filePath = [self applicationDocumentsDirectoryFileName:@"json"];
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath]; //第一次添加数据时,数组为空
if (tempArray.count == ) {
tempArray = [NSMutableArray array];
}

  //文件名(utf-8编码)
NSString *path = [[NSBundle mainBundle] pathForResource:@"city2" ofType:@"txt"];
NSString *contents = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *list = [contents componentsSeparatedByString:@","]; for (NSString *tempStr in list) {
NSDictionary *dict = [tempStr propertyListFromStringsFileFormat];
[tempArray addObject:dict];
} BOOL success = [tempArray writeToFile:filePath atomically:YES];
NSLog(@"success:%d",success);
} #pragma mark - 获取沙盒中的Plist文件路径
- (NSString *)applicationDocumentsDirectoryFileName:(NSString *)fileName
{
NSString *filePath;
//沙盒中的Document文件夹
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; //获取传来的Plist文件名
NSString *fileNameStr = [NSString stringWithFormat:@"%@.plist",fileName];
filePath = [documentDirectory stringByAppendingPathComponent:fileNameStr];
return filePath;
}
//txt文本格式示例:
{id = ;contents = "北京市";},{id = ;contents = "天津市";},{id = ;contents = "石家庄市";},{id = ;contents = "阿勒泰地区";}

把txt文件中的json字符串写到plist文件中的更多相关文章

  1. json数据处理:读取文件中的json字符串,转为python字典

    方法1: 读取文件中的json字符串, 再用json.loads转为python字典 import json str_file = './960x540/config.json' with open( ...

  2. 在java代码中显示json字符串(怎么避免json字符串中双引号在java代码中显示)

    String log = "eyJvcmRlckluZm8iOnsiaWQiOjEwNzQwNCwib3JkZXJJZCI6MjczNjQyMSwicHJvZHVjdENvZGUiOjQ1N ...

  3. struts2:JSON在struts中的应用(JSP页面中将对象转换为JSON字符串提交、JSP页面中获取后台Response返回的JSON对象)

    JSON主要创建如下两种数据对象: 由JSON格式字符串创建,转换成JavaScript的Object对象: 由JSON格式字符串创建,转换成JavaScript的List或数组链表对象. 更多关于J ...

  4. 【spring boot】在自定义拦截器中从request中获取json字符串

    又这样的需求,需要在自定义的拦截器中获取request中的数据,想获取到的是JSON字符串 那需要在拦截器中写这样一个方法 public static String getOpenApiRequest ...

  5. delegate 集成在类中,还是单独写在.h文件中?

    转:http://stackoverflow.com/questions/11382057/declaring-a-delegate-protocol There definitely are sub ...

  6. js中把JSON字符串转换成JSON对象最好的方法

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 第一种解析方式:使用eval函数来解析,并且使用j ...

  7. 【JSON】JSON字符串的操作(不断积累中)

    一.JS遍历JSON串 示例01 目标:返回的JSON串中,一个Key对应的Value是一个数组(若在Java程序中,是一个List). 现在要求获取每个数组中的第一个对象元素中的name属性的值. ...

  8. Js中把JSON字符串转换为JSON对象(eval()、new Function())

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 第一种解析方式:使用eval函数来解析,并且使用j ...

  9. JavaScript中把Json字符串转化为对象

    1.采用eval()函数 定义和用法 eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 语法 eval(string) 参数 描述 string 必需.要计算的字符串, ...

随机推荐

  1. Web性能优化之图片优化

    http://get.jobdeer.com/6513.get 其中APNG和WebP格式出现的较晚,尚未被Web标准所采纳,只有在特定平台或浏览器环境可以预知的情况下加以采用,虽然均可以在不支持的环 ...

  2. js 原型的内存分析

    使用构造器的弊端:http://www.cnblogs.com/a757956132/p/5258897.html 示例 将行为设置为全局的行为,如果将所有的方法都设计为全局函数的时候, 这个函数就可 ...

  3. PL/pgSQL多输出参数例子

    例子一,不带returns: postgres=# CREATE FUNCTION sum_n_product(x int, y int, OUT sum int, OUT prod int) AS ...

  4. [置顶] Android开发之MediaPlayerService服务详解(一)

    前面一节我们分析了Binder通信相关的两个重要类:ProcessState 和 IPCThreadState.ProcessState负责打开Binder 驱动,每个进程只有一个.而 IPCThre ...

  5. shader 语言 【转】

    shader语言 3dlabs改名后其开发者网站关闭 可以在这里下载shadergen http://3dshaders.com/home/index.php?option=com_weblinks& ...

  6. Android应用增量更新

    Original:https://github.com/cundong/SmartAppUpdates Backup:https://github.com/eltld/SmartAppUpdates

  7. javascript异步延时载入及推断是否已载入js/css文件

    <html> <head> <script type="text/javascript"> /**======================= ...

  8. poj 2688 状态压缩dp解tsp

    题意: 裸的tsp. 分析: 用bfs求出随意两点之间的距离后能够暴搜也能够用next_permutation水,但效率肯定不如状压dp.dp[s][u]表示从0出发訪问过s集合中的点.眼下在点u走过 ...

  9. WCF摘记

    //绑定形式 NetTcpBinding bind = new NetTcpBinding(); //地址 EndpointAddress address = new EndpointAddress( ...

  10. javascript代码解释执行过程

    javascript是由浏览器解释执行的脚本语言,不同于java c,需要先编译后运行,javascript 由浏览器js解释器进行解释执行,总的过程分为两大块,预编译期和执行期 下面的几个demo解 ...