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开发之把 Array 和 Dictionaries 序列化成 JSON 对象的更多相关文章

  1. jQuery表单验证以及将表单序列化为json对象小练习

    jquery表单验证(非实时验证),同时,将表单序列化为json对象提交表单. <!DOCTYPE html> <html lang="en"> <h ...

  2. C#将对象序列化成JSON字符串

    C#将对象序列化成JSON字符串 public string GetJsonString() { List<Product> products = new List<Product& ...

  3. 将表单序列化为JSON对象

    将表单序列化为JSON对象的工具方法: $(function() { //工具方法,可以将指定的表单中的输入项目序列化为JSON数据 $.fn.serializeJson = function() { ...

  4. SpringMVC将表单对象序列化成Json字符串提交,以List接收

    出自:http://blog.csdn.net/m0_37595732/article/details/71440853 HTML <%@ page language="java&qu ...

  5. 023-将表单序列化为json对象

    使用jQuery将表单序列化为json对象,其中serializeJson方法的名字任意,serializeArray()这个jQuery提供的方法.this指的就是谁调用了这个方法. $.fn.se ...

  6. 将序列化成json格式的日期(毫秒数)转成日期格式

    <script> $(function () { loadInfo(); }) function loadInfo() { $.post("InfoList.ashx" ...

  7. 使用 EntityFramework后把一个对象序列化成json字符串引起循环引用的问题

    先看一个T4模板生成的model实体类 著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者:卷猫 链接:http://anneke.cn/ArticleInfo/Detial ...

  8. 将序列化成json格式后日期(毫秒数)转成日期格式

    System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaS ...

  9. C#中类的字段或属性不被序列化成JSON或XML

    将一个类序列化成JSON或XML时,如果某个字段或属性不想被序列化,则可以使用以下Attribute: 1.[Newtonsoft.Json.JsonIgnore]特性:使用Newtonsoft.Js ...

随机推荐

  1. Android 控件EditText的setOnEditorActionListener方法的理解

    需要注意的是 setOnEditorActionListener这个方法,并不是在我们点击EditText的时候触发,也不是在我们对EditText进行编辑时触发,而是在我们编辑完之后点击软键盘上的回 ...

  2. c#List泛型数据扩展,把List&lt;&gt;型数据格式化成List&lt;SelectListItem&gt;,用来作dropdownlist的数据

    代码例如以下 public static List<SelectListItem> CreateSelect<T>(this IList<T> t, string ...

  3. udp绑定信息

    1. udp网络程序-端口问题 会变的端口号 重新运行多次脚本,然后在“网络调试助手”中,看到的现象如下: 说明: 每重新运行一次网络程序,上图中红圈中的数字,不一样的原因在于,这个数字标识这个网络程 ...

  4. Codeforces 491B. New York Hotel 最远曼哈顿距离

    最远曼哈顿距离有两个性质: 1: 对每一个点(x,y)  分别计算  +x+y , -x+y , x-y , -x-y 然后统计每种组合的最大值就能够了, 不会对结果产生影响 2: 去掉绝对值 , 设 ...

  5. 【t005】数字构造问题

    Time Limit: 1 second Memory Limit: 50 MB [问题描述] 给定一个只包含数字[0..9]的字符串,请使用字符串中的某些字符,构建一个能够整除15最大的整数.注意, ...

  6. MongoDB Shell 经常使用操作

    数组查询 数组查询 MongoDB 中有子文档的概念.一个文档中能方便的嵌入子文档,这与关系性数据库有着明显的不同,在查询时,语法有一些注意点. 样例代码,假如我们的一个集合(tests)中存在标签键 ...

  7. [Angular] @ViewChildren and QueryLists (ngAfterViewInit)

    When you use @ViewChildren, the value can only be accessable inside ngAfterViewInit lifecycle. This ...

  8. duplicate symbols for architecture

    duplicate symbols for architecture   XCODE编译的时候报错:duplicate symbols for architecture armv7   1.首先排查是 ...

  9. [搜索]Trie树的实现

    trie这种树也被称为线索,搜索树. 正如图 以下是用stl 的map来实现 class trie_item_c { public: trie_item_c(){} trie_item_c(const ...

  10. fatal error: expat.h: No such file or directory

    在CentOS7最小安装版下,编译安装apr-util时报错: fatal error: expat.h: No such file or directory 解决办法:yum install exp ...