3

我有一个任务要求我将字符串作为 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 写入中的顶级类型无效”

有人可以帮我吗?

4

2 回答 2

12

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

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

让你的 NSData 像这样:

NSData *jsonPayload = [json dataUsingEncoding:NSUTF8StringEncoding];
于 2012-04-20T10:23:53.017 回答
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":""}
于 2012-04-20T10:24:08.773 回答