欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS开发(74)之把 Array 和 Dictionaries 序列化成 JSON 对象

程序员文章站 2023-10-27 14:04:04
1 前言 通过 nsjsonserialization 这个类的 datawithjsonobject:options:error:方法来实现,array 和 dictionar...

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"

}