将字符串作为JSON对象发送

时间:2012-04-20 10:02:33

标签: iphone objective-c ios5

我有一个赋值,要求我把一个字符串作为一个json对象,在发送对象之前我必须将这个json对象放入一个http头。这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *nid = @"";
    NSString *vocab = @"";
    NSString *inturl = @"testoverview";
    NSString *mail = @"chh@fbr.dk";
    NSString *md5pw = @"4d57e7ef1b7c3f431aca424764e9d786";

    NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                    nid, @"nid", 
                                    vocab, @"vocab",
                                    inturl, @"inturl",
                                    mail, @"mail",
                                    md5pw, @"md5pw",nil];


    NSString *json = @"{nid:"",vocab:"",inturl:testoverview, mail:chh@fbr.dk, md5pw:4d57e7ef1b7c3f431aca424764e9d786}";


    NSError *error;



    NSString *url = @"http://udv.taenk.dk/chh/drupal-taenk/services/mobile";


    NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error];


    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:30.0];

     NSURLConnection *connction = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    FSRemoteExecutor *remote = [[FSRemoteExecutor alloc] init];
    [remote execute:url andHandler:[FSProductTestHandler alloc] JSONString:jsonString JSONData:jsonData Connection:connction];
    [remote connection:connction didReceiveData:jsonData];
    [remote connectionFinishedLoading:connction];

我的问题是我不能将jsonDictionary与对象一起使用并发送它,因为服务必须接收的字符串格式是:

“{”nid“:”“,”vocab“:”“,”inturl“:”testoverview“,”mail“:”“,”md5pw“:”“}”

字典会在字符串中插入=,这样就不会给我服务的响应。

我想发送一个字符串(代码中的json)作为dataWithJSONObject:jso,喜欢这个:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:&error];

但是我得到了一个错误:

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* + [NSJSONSerialization dataWithJSONObject:options:error:]:JSON写入中的顶级类型无效'

任何人都可以帮我这个吗?

2 个答案:

答案 0 :(得分:12)

但为什么要使用NSJSONSerialization呢? 你做了自己的JSON字符串(NSString *json = @"{nid:"",vocab:"",inturl:testoverview, mail:chh@fbr.dk, md5pw:4d57e7ef1b7c3f431aca424764e9d786}";)。然后您无需再次运行NSJSONSerialization

只需从*json (NSString)中创建一个NSData,然后将其发送到服务器。

使你的NSData像这样:

NSData *jsonPayload = [json dataUsingEncoding:NSUTF8StringEncoding];

答案 1 :(得分:4)

首先,您的字符串json不包含您认为它的引号字符。您必须使用反斜杠“转义”字符串中的引号,以使其不终止字符串。你的行:

NSString *json = @"{nid:"",vocab:"",inturl:testoverview, mail:chh@fbr.dk, md5pw:4d57e7ef1b7c3f431aca424764e9d786}";
正在解析

NSString *json = @"{nid:"
                 ",vocab:"
                 ",inturl:testoverview, mail:chh@fbr.dk, md5pw:4d57e7ef1b7c3f431aca424764e9d786}";

也就是说,它依次是三个引用的字符串。编译器连接相邻的字符串文字。所以,效果是这样的:

NSString *json = @"{nid:,vocab:,inturl:testoverview, mail:chh@fbr.dk, md5pw:4d57e7ef1b7c3f431aca424764e9d786}";

没有嵌入的引号字符。

你想写的是:

NSString *json = @"{nid:\"\",vocab:\"\",inturl:testoverview, mail:chh@fbr.dk, md5pw:4d57e7ef1b7c3f431aca424764e9d786}";

您能否显示输出:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error];

我看不到你会得到一个“=”字符。


更新

此代码(根据您的问题修改):

NSString *nid = @"";
NSString *vocab = @"";
NSString *inturl = @"testoverview";
NSString *mail = @"chh@fbr.dk";
NSString *md5pw = @"4d57e7ef1b7c3f431aca424764e9d786";

NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                nid, @"nid", 
                                vocab, @"vocab",
                                inturl, @"inturl",
                                mail, @"mail",
                                md5pw, @"md5pw",nil];

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString *resultAsString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"jsonData as string:\n%@", resultAsString);

给出了这个输出:

jsonData as string:
{
  "md5pw" : "4d57e7ef1b7c3f431aca424764e9d786",
  "nid" : "",
  "inturl" : "testoverview",
  "mail" : "chh@fbr.dk",
  "vocab" : ""
}

如果您使用0代替NSJSONWritingPrettyPrinted,则会提供:

jsonData as string:
{"md5pw":"4d57e7ef1b7c3f431aca424764e9d786","nid":"","inturl":"testoverview","mail":"chh@fbr.dk","vocab":""}