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

IOS开发(75)之把 JSON 数据转化成 Arrays 或者 Dictionaries

程序员文章站 2023-10-27 13:30:10
1 前言 通过 nsjsonserialization 这个类的 jsonobjectwithdata:options:error:方法来实现,把json 数据解析出来放在数据或...

1 前言
通过 nsjsonserialization 这个类的 jsonobjectwithdata:options:error:方法来实现,把json 数据解析出来放在数据或者字典里面保存。


2 代码示例
testdemo.m

 

[plain]
-(void)convsefromjson{ 
    nsmutabledictionary *dictionary = [[nsmutabledictionary alloc] init]; 
    [dictionary setvalue:@"anthony" forkey:@"first name"]; 
    [dictionary setvalue:@"robbins" forkey:@"last name"]; 
    [dictionary setvalue:[nsnumber numberwithunsignedinteger:51] forkey:@"age"]; 
    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]; 
    [dictionary setvalue:arrayofanthonyschildren forkey:@"children"]; 
    nserror *error = nil; 
    nsdata *jsondata = [nsjsonserialization 
                        datawithjsonobject:dictionary options:nsjsonwritingprettyprinted error:&error]; 
    if ([jsondata length] > 0 && error == nil){ 
        nslog(@"successfully serialized the dictionary into data."); 
        /* json转数组/字典 */ 
        error = nil; 
        //转换方法 
        id jsonobject = [nsjsonserialization 
                         jsonobjectwithdata:jsondata options:nsjsonreadingallowfragments 
                         error:&error]; 
        if (jsonobject != nil && error == nil){ 
            nslog(@"successfully deserialized..."); 
            //如果jsonobject是字典类 
            if ([jsonobject iskindofclass:[nsdictionary class]]){ 
                nsdictionary *deserializeddictionary = (nsdictionary *)jsonobject; 
                nslog(@"dersialized json dictionary = %@", deserializeddictionary); 
            } 
            //如果jsonobject是数组类 
            else if ([jsonobject iskindofclass:[nsarray class]]){ 
                nsarray *deserializedarray = (nsarray *)jsonobject; 
                nslog(@"dersialized json array = %@", deserializedarray); 
            } else { 
                nslog(@"i can't deal with it"); 
            } 
        } 
        else if (error != nil){ 
            nslog(@"an error happened while deserializing the json data."); } 
    } 
    else if ([jsondata length] == 0 &&error == nil){ 
        nslog(@"no data was returned after serialization."); 
    } 
    else if (error != nil){ 
        nslog(@"an error happened = %@", error); 
    } 

-(void)convsefromjson{
    nsmutabledictionary *dictionary = [[nsmutabledictionary alloc] init];
    [dictionary setvalue:@"anthony" forkey:@"first name"];
    [dictionary setvalue:@"robbins" forkey:@"last name"];
    [dictionary setvalue:[nsnumber numberwithunsignedinteger:51] forkey:@"age"];
    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];
    [dictionary setvalue:arrayofanthonyschildren forkey:@"children"];
    nserror *error = nil;
    nsdata *jsondata = [nsjsonserialization
                        datawithjsonobject:dictionary options:nsjsonwritingprettyprinted error:&error];
    if ([jsondata length] > 0 && error == nil){
        nslog(@"successfully serialized the dictionary into data.");
        /* json转数组/字典 */
        error = nil;
        //转换方法
        id jsonobject = [nsjsonserialization
                         jsonobjectwithdata:jsondata options:nsjsonreadingallowfragments
                         error:&error];
        if (jsonobject != nil && error == nil){
            nslog(@"successfully deserialized...");
            //如果jsonobject是字典类
            if ([jsonobject iskindofclass:[nsdictionary class]]){
                nsdictionary *deserializeddictionary = (nsdictionary *)jsonobject;
                nslog(@"dersialized json dictionary = %@", deserializeddictionary);
            }
            //如果jsonobject是数组类
            else if ([jsonobject iskindofclass:[nsarray class]]){
                nsarray *deserializedarray = (nsarray *)jsonobject;
                nslog(@"dersialized json array = %@", deserializedarray);
            } else {
                nslog(@"i can't deal with it");
            }
        }
        else if (error != nil){
            nslog(@"an error happened while deserializing the json data."); }
    }
    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:26:15.726 fromjsontest[4944:303] successfully serialized the dictionary into data.

2013-05-13 17:26:15.728 fromjsontest[4944:303] successfully deserialized...

2013-05-13 17:26:15.728 fromjsontest[4944:303] dersialized json dictionary = {

    age = 51;

    "first name" = anthony;

    "last name" = robbins;

    children =     (

        "anthony's son 1",

        "anthony's daughter 1",

        "anthony's son 2",

        "anthony's son 3",

        "anthony's daughter 2"

    );