zoukankan      html  css  js  c++  java
  • IOS开发之把 Array 和 Dictionaries 序列化成 JSON 对象

    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"

    }

  • 相关阅读:
    错误
    分页查询
    异步请求jquery
    深入理解C/C++ [Deep C (and C++)]
    C语言经典算法100例(三)
    《Python》 计算机基础
    Python程序员的进化史
    以前没有写笔记的习惯,现在慢慢的发现及时总结是多么的重要。 这一篇文章主要关于java多线程一些常见的疑惑点。因为讲解多线程的书籍和文章已经很多了,所以我也不好意思多说,嘻嘻嘻、大家可以去参考一些那些书籍。我这个文章主要关于实际的一些问题。同时也算是我以后复习的资料吧,。还请大家多多指教。 同时希望多结交一些技术上的朋友。谢谢。
    快速读入函数
    一元二次方程公式
  • 原文地址:https://www.cnblogs.com/Xer-Lee/p/3154849.html
Copyright © 2011-2022 走看看