- (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. Rank() over(partition ... 分组统计的实例

    USE [NanFeng]GO/****** Object: StoredProcedure [dbo].[st_MES_RptMaterilSum] Script Date: 04/18/2016 ...

  2. STL中vector容器实现反转(reverse)

    vector容器中实现可以通过以下两种方式实现: #include "stdafx.h" #include <vector> #include <iostream ...

  3. CloudSTack4.2 查看所有虚拟机API测试

    http://192.168.153.34:8080/ client/api? command=listVirtualMachines &response=json &sessionk ...

  4. spring中propertyplaceholderconfigurer简介

    Spring的框架中为您提供了一个 BeanFactoryPostProcessor 的实作类别: org.springframework.beans.factory.config.PropertyP ...

  5. Android多线程研究(1)——线程基础及源代码剖析

    从今天起我们来看一下Android中的多线程的知识,Android入门easy,可是要完毕一个完好的产品却不easy,让我们从线程開始一步步深入Android内部. 一.线程基础回想 package ...

  6. MySQL中进行树状所有子节点的查询

    在Oracle 中我们知道有一个 Hierarchical Queries 通过CONNECT BY 我们可以方便的查了所有当前节点下的所有子节点.但很遗憾,在MySQL的目前版本中还没有对应的功能. ...

  7. 【转】linux下awk内置函数的使用(split/substr/length)

    转自:http://gdcsy.blog.163.com/blog/static/12734360920130241521280/ 一.split 初始化和类型强制        awk的内建函数sp ...

  8. imindmap7_windows_7.0

    思维导图工具: imindmap7_windows_7.0 iMindMap7.0 和谐包V1.0 22:27:23

  9. C++ CreateDirectory

    创建文件夹 关键点 CreateDirectory The CreateDirectory function creates a new directory. If the underlying fi ...

  10. Eclipse中SVN的安装步骤(两种)和用法

    一.给安装EclipseSVN,最常见的有两种方式:手动方式和使用安装向导方式.详细过程例如以下: 方式一:手动安装 1.从官网下载site-1.6.9.zip文件,网址是:subclipse.tig ...