• Convert an object into Json using SBJson or other JSON library


    Using SBJson, to convert a object to JSON string, you have to override the proxyForJson method. Like the following,

    The .h file,

    @interface MyCustomObject : NSObject {
       
    NSString *receiverFirstName;
       
    NSString *receiverMiddleInitial;
       
    NSString *receiverLastName;
       
    NSString *receiverLastName2;
    }
    @property (nonatomic, retain) NSString *receiverFirstName;
    @property (nonatomic, retain) NSString *receiverMiddleInitial;
    @property (nonatomic, retain) NSString *receiverLastName;
    @property (nonatomic, retain) NSString *receiverLastName2;

    - (id) proxyForJson;
    - (int) parseResponse :(NSDictionary *) receivedObjects;
    }

    In the implementation file,

        - (id) proxyForJson {

           
    return [NSDictionary dictionaryWithObjectsAndKeys:
                receiverFirstName
    , @"ReceiverFirstName",
                receiverMiddleInitial
    , @"ReceiverMiddleInitial",
                receiverLastName
    , @"ReceiverLastName",
                receiverLastName2
    , @"ReceiverLastName2",
                nil
    ];
       
    }

    And to get the object from the JSON string you have to write a parseResponse method like this,

    - (int) parseResponse :(NSDictionary *) receivedObjects {
        self
    .receiverFirstName = (NSString *) [receivedObjects objectForKey:@"ReceiverFirstName"];
        self
    .receiverLastName = (NSString *) [receivedObjects objectForKey:@"ReceiverLastName"];

       
    /* middleInitial and lastname2 are not required field. So server may return null value which
         eventually JSON parser return NSNull. Which is unrecognizable by most of the UI and functions.
         So, convert it to empty string. */

       
    NSString *middleName = (NSString *) [receivedObjects objectForKey:@"ReceiverMiddleInitial"];
       
    if ((NSNull *) middleName == [NSNull null]) {
            self
    .receiverMiddleInitial = @"";
       
    } else {
            self
    .receiverMiddleInitial = middleName;
       
    }

       
    NSString *lastName2 = (NSString *) [receivedObjects objectForKey:@"ReceiverLastName2"];
       
    if ((NSNull *) lastName2 == [NSNull null]) {
            self
    .receiverLastName2 = @"";
       
    } else {
            self
    .receiverLastName2 = lastName2;
       
    }

       
    return 0;
    }
  • 相关阅读:
    Vulnhub-靶机-BREACH: 3.0.1
    sqlmap从入门到精通-第六章-6-3 SOAP注入某SQL 2008服务器结合MSF进行提权
    sqlmap从入门到精通-第六章-6-2 使用sqlmap曲折渗透某服务器
    service xxx does not support chkconfig
    CentOS7安装ActiveMQ
    centos6.4 搭建svn服务器
    关于ZYNQ-7000中断调试一点感想
    闲谈SQL脚本优化
    小朋友学C语言(1):Hello World
    Python基础1
  • 原文地址:https://www.cnblogs.com/neozhu/p/2431890.html
Copyright © 2020-2023  润新知