iphone,SBJsonWriter ..转换对象

时间:2011-05-27 12:47:24

标签: xcode json object

我想将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);

请帮忙。我不知道上面的代码有什么问题。

2 个答案:

答案 0 :(得分:2)

要弄清楚为什么你的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
}

注意:

  1. 使用'错误'让我弄清楚如果你写[NSDate 日期]而不是[[NSDate日期]描述]你会得到一个“JSON 不支持序列化 __NSTaggedDate“错误。
  2. 注意浮动舍入错误... 1.2345变为1.2345000505447388

答案 1 :(得分:0)

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