iphone,SBJsonWriter ..转换对象

| 我想将json对象发布到请求。但是JSONWriter似乎并没有转换我的对象,也不知道我在做什么错... 这是我的转换代码。
    customer = [[Customer alloc] init];

Address *tempAddr = [[Address alloc] init];
tempAddr.Line1=strAddr1 ;
tempAddr.Zip =strPost;
tempAddr.City =strStreet;
[customer setInvoiceAddress:tempAddr];

customer.FirstName=strFirstName;
customer.LastName=strLastName;
customer.CellPhone=strCellPhone;
customer.Phone=strPhone;
customer.Email=strEmail;

SBJsonWriter *writer = [SBJsonWriter alloc];
NSString *jsonConvertedObj = [writer stringWithObject:customer];
NSLog(@\"the json converted object ... %@\", jsonConvertedObj);
请帮忙。我不知道上面的代码有什么问题。     
已邀请:
        AFAIK SBJSONWriter仅支持将字典和数组转换为JSON字符串。它不适用于任意对象。 编辑:这是相应的实现,如果提供的对象既不是字典也不是数组,则返回nil。
- (NSString*)stringWithObject:(id)value {
    if ([value isKindOfClass:[NSDictionary class]] || [value isKindOfClass:[NSArray class]]) {
        return [self stringWithFragment:value];
    }

    [self clearErrorTrace];
    [self addErrorWithCode:EFRAGMENT description:@\"Not valid type for JSON\"];
    return nil;
}
    
        为了弄清楚为什么Json解析失败,您应该使用以下方法:
    NSError *error = nil;
    NSString * jsonTest = [[[SBJsonWriter alloc] init] stringWithObject:jsonTestDictionary error:&error];

    if ( ! jsonTest ) {
        NSLog(@\"Error: %@\", error);
    }else{
        NSLog(@\"%@\", jsonTest);
    } 
这是一个演示如何使用它的简单代码:
#import <Foundation/Foundation.h>

#import \"SBJsonWriter.h\"
#import \"SBJsonParser.h\"


int main(int argc, const char * argv[])
{

    @autoreleasepool {

        NSDictionary* aNestedObject = [NSDictionary dictionaryWithObjectsAndKeys:
                                              @\"nestedStringValue\", @\"aStringInNestedObject\",
                                              [NSNumber numberWithInt:1], @\"aNumberInNestedObject\",
                                         nil];

        NSArray * aJSonArray = [[NSArray alloc] initWithObjects: @\"arrayItem1\", @\"arrayItem2\", @\"arrayItem3\", nil];

        NSDictionary * jsonTestDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                             @\"stringValue\", @\"aString\",
                                             [NSNumber numberWithInt:1], @\"aNumber\",
                                             [NSNumber numberWithFloat:1.2345f], @\"aFloat\",
                                             [[NSDate date] description], @\"aDate\",
                                             aNestedObject, @\"nestedObject\",
                                             aJSonArray, @\"aJSonArray\",
                                             nil];

        // create JSON output from dictionary

        NSError *error = nil;
        NSString * jsonTest = [[[SBJsonWriter alloc] init] stringWithObject:jsonTestDictionary error:&error];

        if ( ! jsonTest ) {
            NSLog(@\"Error: %@\", error);
        }else{
            NSLog(@\"%@\", jsonTest);
        } 
    }
    return 0;
}
输出
{
    \"aDate\":\"2012-09-12 07:39:00 +0000\",
    \"aFloat\":1.2345000505447388,
    \"nestedObject\":{
        \"aStringInNestedObject\":\"nestedStringValue\",
        \"aNumberInNestedObject\":1
     },
    \"aJSonList\":[\"arrayItem1\",\"arrayItem2\",\"arrayItem3\"],
    \"aString\":\"stringValue\",
    \"aNumber\":1
}
注意: 使用\'error \'可以让我弄清楚,如果您编写[NSDate 日期]而不是[[NSDate日期]描述],您将获得一个\“ JSON 不支持序列化 __NSTaggedDate \”错误。 注意float舍入错误... 1.2345变为1.2345000505447388     

要回复问题请先登录注册