- (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. 【OpenGL】glFinish()和glFlush()函数详解-[转]

    通常情况下,OpenGL指令不是立即执行的.它们首先被送到指令缓冲区,然后才被送到硬件执行.glFinish和glFlush都是强制将命令缓冲区的内容提交给硬件执行. 一.glFinish()函数   ...

  2. 从UnitedStack OS 1.0 Preview试用申请问卷调查学习OpenStack

    http://www.diaochapai.com/survey/ 您的角色最可能是? * (必填, 多选) OpenStack私有云用户,希望能将OpenStack/UOS用于公司内部私有云 云计算 ...

  3. 分布式文件系统FastDFS设计原理

    原文地址: http://blog.chinaunix.net/uid-20196318-id-4058561.html FastDFS是一个开源的轻量级分布式文件系统,由跟踪服务器(tracker ...

  4. Mailbox and Mail

    #ifndef __MAILBOX_H__ #define __MAILBOX_H__ #include <stdint.h> #include <stdlib.h> #inc ...

  5. 负margin使用权威指南

    自CSS2早在1998年,推荐表的使用已经慢慢褪色成背景和历史书中.正因为如此,CSS布局从那时起一直编码优雅的代名词. 的所有CSS概念设计师所使用,奖项可能需要给负margin的使用是最至少谈论的 ...

  6. iOS 3DES DES AES加密注意事项!!很重要,否则会加密失败

    今天做项目,需要进行3des加密. 加密的gkey:abcdefgh   giv:(偏移量)abcdefgh 加密后结果:p+X985x5bFS6dWjAnm6sdQ== 下面是代码: +(NSStr ...

  7. Python在Windows上的安装

    1:下载 http://dlsw.baidu.com/sw-search-sp/soft/6e/17016/python-3.3.5.1395976247.msi 2:直接next安装 3:验证安装是 ...

  8. 使用NuGet安装EntityFramework4.2

    1.下载NuGet 有两种方式下载NuGet 第一种:在微软的档案库下载,下载地址为:http://visualstudiogallery.msdn.microsoft.com/27077b70-9d ...

  9. [ES6] 15. Generators -- 2

    Using for..of statement: function* greeting(){ console.log(`Generators are "lazy"`); yield ...

  10. java Date比较

    package com.horizon.test; import org.apache.commons.lang.time.DateUtils; public class Hello3 { publi ...