zoukankan      html  css  js  c++  java
  • Conversion between json and object using SBJson lib

    Define two methods in an object class as follows:

     

    @interface MyObject : NSObject
    @property (nonatomic,copy) NSString *property;
    
    - (id)initWithJson:(NSDictionary *)item;
    - (id)proxyForJson;
    
    - (id)initWithJson:(NSDictionary *)item {  
       self = [superinit];
       if (self) {
           self.property = [itemobjectForKey:@"Property"];
        }
        return self;
    }
    
    - (id) proxyForJson {
        return [NSDictionarydictionaryWithObjectsAndKeys:property,@"Property",
                nil];
    }

    • Convert json to object

     

       NSData *dataReply = [NSURLConnectionsendSynchronousRequest:urlRequest returningResponse:&response error:&error];
    
       NSDictionary *results = [dataReply JSONValue];
        
       MyObject *object = [[MyObjectalloc] initWithJson:results];


     

    • Convert object to json string
    MyObject *object = [[MyObjectalloc]init];
    NSString*json = [objectJSONRepresentation];





     


  • 相关阅读:
    pat00-自测5. Shuffling Machine (20)
    Spiral Matrix
    Search in Rotated Sorted Array II
    Search in Rotated Sorted Array
    Best Time to Buy and Sell Stock II
    4Sum
    3Sum Closest
    3Sum
    MySQL存储过程、函数和游标
    Word Ladder
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3271335.html
Copyright © 2011-2022 走看看