【转】NSJSONSerialization解析JSON数据

简介:

JSON->NSData

复制代码

 1 - (IBAction)touchWriteButton:(id)sender {
2 NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
3 [dictionary setValue:@"Anthony" forKey:@"First Name"];
4 [dictionary setValue:@"Robbins" forKey:@"Last Name"];
5 [dictionary setValue:[NSNumber numberWithUnsignedInteger:51] forKey:@"Age"];
6 NSArray *arrayOfAnthonysChildren = [[NSArray alloc] initWithObjects:@"Anthony's Son 1", @"Anthony's Daughter 1", @"Anthony's Son 2", @"Anthony's Son 3", @"Anthony's Daughter 2", nil];
7 [dictionary setValue:arrayOfAnthonysChildren forKey:@"children"];
8 NSError *error = nil;
9 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
10 if (error) {
11 NSLog(@"dic->%@",error);
12 }
13 [dictionary release];
14 BOOL succeed = [jsonData writeToFile:JSON_PATH atomically:YES];
15 if (succeed) {
16 NSLog(@"Save succeed");
17 }else {
18 NSLog(@"Save fail");
19 }
20 }
 
NSData->JSON
 1 - (IBAction)touchReadButton:(id)sender { 
2 NSData *jsonData = [[NSData alloc] initWithContentsOfFile:JSON_PATH]; /* Now try to deserialize the JSON object into a dictionary */
3 NSError *error = nil;
4 id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
5 if (jsonObject != nil && error == nil){
6 NSLog(@"Successfully deserialized...");
7 if ([jsonObject isKindOfClass:[NSDictionary class]]){
8 NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
9 NSLog(@"Dersialized JSON Dictionary = %@", deserializedDictionary);
10 } else if ([jsonObject isKindOfClass:[NSArray class]]){
11 NSArray *deserializedArray = (NSArray *)jsonObject;
12 NSLog(@"Dersialized JSON Array = %@", deserializedArray);
13 } else {
14 NSLog(@"An error happened while deserializing the JSON data.");
15 }
16 }
17 [jsonData release];
18 }

 本文转自编程小翁博客园博客,原文链接:http://www.cnblogs.com/wengzilin/archive/2012/07/17/2595683.html,如需转载请自行联系原作者

相关文章
|
29天前
|
JSON JavaScript 前端开发
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
264 0
|
12天前
|
存储 JSON JavaScript
「Python系列」Python JSON数据解析
在Python中解析JSON数据通常使用`json`模块。`json`模块提供了将JSON格式的数据转换为Python对象(如列表、字典等)以及将Python对象转换为JSON格式的数据的方法。
28 0
|
15天前
|
存储 JSON 数据挖掘
python逐行读取txt文本中的json数据,并进行处理
Python代码示例演示了如何读取txt文件中的JSON数据并处理。首先,逐行打开文件,然后使用`json.loads()`解析每一行。接着,处理JSON数据,如打印特定字段`name`。异常处理包括捕获`JSONDecodeError`和`KeyError`,确保数据有效性和字段完整性。将`data.txt`替换为实际文件路径运行示例。
12 2
|
30天前
|
JSON JavaScript 数据格式
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
87 2
|
30天前
|
XML JSON API
深入解析C++ JSON库:nlohmann::json:: parse的内部机制与应用
深入解析C++ JSON库:nlohmann::json:: parse的内部机制与应用
48 0
|
1月前
|
JSON 数据格式
糊涂工具类(hutool)post请求设置body参数为json数据
糊涂工具类(hutool)post请求设置body参数为json数据
40 1
|
7月前
|
JSON Java Maven
在Java中,我们可以使用第三方库来解析JSON数据
在Java中,我们可以使用第三方库来解析JSON数据。这里以常用的Jackson库为例,演示如何解析JSON数据。
58 1
|
4月前
|
SQL JSON 关系型数据库
【SQL编程】MySQL 5.7.28 版本使用 SQL 直接解析 JSON 字符串(判断是否是合法JSON类型+文本深度+文本长度+值类型+keys获取+值获取+不同深度数据获取)
【SQL编程】MySQL 5.7.28 版本使用 SQL 直接解析 JSON 字符串(判断是否是合法JSON类型+文本深度+文本长度+值类型+keys获取+值获取+不同深度数据获取)
51 0
|
4月前
|
JSON 数据格式
这个错误是由于在解析JSON数据时出现了问题。你可以尝试使用`try-except`语句来捕获异常
这个错误是由于在解析JSON数据时出现了问题。你可以尝试使用`try-except`语句来捕获异常
37 2
|
4月前
|
数据采集 JSON JavaScript
C# 解析“JSON“格式数据和网络实战案例 入门
C# 解析“JSON“格式数据和网络实战案例 入门

推荐镜像

更多