简体   繁体   English

使用SBJson或其他JSON库将对象转换为Json

[英]Convert an object into Json using SBJson or other JSON library

我需要一个易于使用的库whit示例,用于将NSObject转换为JSON并再次返回,我在网上发现了大量用于解析JSon的解析示例但在使用SBJSON将NSObject转换为JSON时没有太多,Anybody正文有一个很好的教程或者将NSObject转换为JSON的示例代码?

With SBJSON, it's really simple. 使用SBJSON,它非常简单。

NSString *myDictInJSON = [myDict JSONRepresentation];
NSString *myArrayInJSON = [myArray JSONRepresentation];

Of course, to go the other way array, do: 当然,要走另一条路阵,请:

NSDictionary *myDict = [myDictInJSON JSONValue];
NSArray *myArray = [myArrayInJSON JSONValue];

Using SBJson , to convert a object to JSON string, you have to override the proxyForJson method. 使用SBJson将对象转换为JSON字符串,您必须覆盖proxyForJson方法。 Like the following, 如下,

The .h file, .h文件,

@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, 要从JSON字符串中获取对象,您必须编写这样的parseResponse方法,

- (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;
}

From JSON String to Objects: 从JSON字符串到对象:

SBJsonParser *parser = [[SBJsonParser alloc] init];

// gives array as output

id objectArray = [parser objectWithString:@"[1,2,3]"]; 

// gives dictionary as output

id objectDictionary = [parser objectWithString:@"{\"name\":\"xyz\",\"email\":\"xyz@email.com\"}"]; 

From Objects to JSON String: 从对象到JSON字符串:

SBJsonWriter *writer = [[SBJsonWriter alloc] init];

id *objectArray = [NSArray arrayWithObjects:@"Hello",@"World", nil];

// Pass an Array or Dictionary object.

id *jsonString = [writer stringWithObject:objectArray]; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM