IOS开发之把 Array 和 Dictionaries 序列化成 JSON 对象
来源:互联网 发布:软件检测工程师 编辑:程序博客网 时间:2024/05/16 11:45
1 前言
通过 NSJSONSerialization 这个类的 dataWithJSONObject:options:error:方法来实现,Array 和 dictionary 序列化成 JSON 对象。方便在网络中传输。
2 代码实例
TestDemo.m
[plain]
(void)converseToJson{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:@"Archy" forKey:@"First Name"];
[dictionary setValue:@"Robbins" forKey:@"Last Name"];
[dictionary setValue:[NSNumber numberWithUnsignedInteger:51] forKey:@"Age"];
NSArray *arrayOfArchysChildren = [[NSArray alloc] initWithObjects:
@"Anthony's Son 1",
@"Anthony's Daughter 1",
@"Anthony's Son 2",
@"Anthony's Son 3",
@"Anthony's Daughter 2", nil];
[dictionary setValue:arrayOfArchysChildren forKey:@"children"];
NSError *error = nil;
//NSJSONWritingPrettyPrinted:指定生成的JSON数据应使用空格旨在使输出更加可读。如果这个选项是没有设置,最紧凑的可能生成JSON表示。
NSData *jsonData = [NSJSONSerialization
dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
if ([jsonData length] > 0 && error == nil){
NSLog(@"Successfully serialized the dictionary into data.");
//NSData转换为String
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON String = %@", jsonString);
}
else if ([jsonData length] == 0 &&
error == nil){
NSLog(@"No data was returned after serialization.");
}
else if (error != nil){
NSLog(@"An error happened = %@", error);
}
}
-(void)converseToJson{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:@"Archy" forKey:@"First Name"];
[dictionary setValue:@"Robbins" forKey:@"Last Name"];
[dictionary setValue:[NSNumber numberWithUnsignedInteger:51] forKey:@"Age"];
NSArray *arrayOfArchysChildren = [[NSArray alloc] initWithObjects:
@"Anthony's Son 1",
@"Anthony's Daughter 1",
@"Anthony's Son 2",
@"Anthony's Son 3",
@"Anthony's Daughter 2", nil];
[dictionary setValue:arrayOfArchysChildren forKey:@"children"];
NSError *error = nil;
//NSJSONWritingPrettyPrinted:指定生成的JSON数据应使用空格旨在使输出更加可读。如果这个选项是没有设置,最紧凑的可能生成JSON表示。
NSData *jsonData = [NSJSONSerialization
dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
if ([jsonData length] > 0 && error == nil){
NSLog(@"Successfully serialized the dictionary into data.");
//NSData转换为String
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON String = %@", jsonString);
}
else if ([jsonData length] == 0 &&
error == nil){
NSLog(@"No data was returned after serialization.");
}
else if (error != nil){
NSLog(@"An error happened = %@", error);
}
}
控制台结果
2013-05-13 17:14:26.087 ToJsonTest[4890:303] Successfully serialized the dictionary into data.
2013-05-13 17:14:26.089 ToJsonTest[4890:303] JSON String = {
"children" : [
"Anthony's Son 1",
"Anthony's Daughter 1",
"Anthony's Son 2",
"Anthony's Son 3",
"Anthony's Daughter 2"
],
"Age" : 51,
"First Name" : "Archy",
"Last Name" : "Robbins"
}
- IOS开发(74)之把 Array 和 Dictionaries 序列化成 JSON 对象
- IOS开发之把 Array 和 Dictionaries 序列化成 JSON 对象
- ios 实现 Array 和 Dictionaries 序列化成 JSON 对象
- Dojo:把对象序列化成为json
- IOS开发(75)之把 JSON 数据转化成 Arrays 或者 Dictionaries
- IOS开发之把 JSON 数据转化成 Arrays 或者 Dictionaries
- IOS开发:把 JSON 数据转化成 Arrays 或者 Dictionaries
- iOS开发:对象直接转化成JSON
- iOS: 把对象直接转化成NSDictionary或JSON
- iOS:把对象直接转化成NSDictionary或JSON
- iOS:把对象直接转化成NSDictionary或JSON
- iOS:把对象直接转化成NSDictionary或JSON
- iOS把对象直接转化成NSDictionary或JSON
- 对象序列化成JSON格式
- Java对象序列化成JSON对象
- C#把对象序列化成二进制文件
- [转]js对象序列化成json数据
- js对象序列化成json字符串
- 黑马程序员--01 c#基础知识
- android ListView
- MDI窗体应用程序
- 在 Codeigniter(CI)中引入外部的JS与CSS的配置及使用
- 系统调用
- IOS开发之把 Array 和 Dictionaries 序列化成 JSON 对象
- Map、Collections、Arrays
- row_number rank dense_rank
- Hibernate Criteria 用法
- 微软编程 管道系统
- VC中使用ADO操作数据库的方法
- nginx rewrite规则语法
- d
- spring mvc 3.2 使用总结