Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS/iPhone上的YAJL-JSON_Iphone_Objective C_Json_Yajl - Fatal编程技术网

iOS/iPhone上的YAJL-JSON

iOS/iPhone上的YAJL-JSON,iphone,objective-c,json,yajl,Iphone,Objective C,Json,Yajl,在我的iPhone应用程序中,我尝试使用JSON库(YAJL)创建一个JSON字符串,其格式如下: {"user": {"name":"Jon", "username":"jon22", "password":"passw@rd", "email":"jon22@example.com"} } 但是我不知道用YAJL方法来创建这个 我尝试了以下方法: NSArray *params = [NSArray arrayWithObjects:@"Jon", @"jon22", @"pass

在我的iPhone应用程序中,我尝试使用JSON库(YAJL)创建一个JSON字符串,其格式如下:

{"user": 
  {"name":"Jon", "username":"jon22", "password":"passw@rd", "email":"jon22@example.com"}
}
但是我不知道用YAJL方法来创建这个

我尝试了以下方法:

 NSArray *params = [NSArray arrayWithObjects:@"Jon", @"jon22", @"passw@rd", @"jon22@gmail.com", nil];
 NSArray *keys = [NSArray arrayWithObjects:@"name", @"username", @"password", @"email", nil];

 NSDictionary *userDictionary = [NSDictionary dictionaryWithObjects:params forKeys:keys];
 NSString *JSONString = [userDictionary yajl_JSONString];
但是,返回的字符串没有包装在外部“user”中。 我如何使用YAJL创建这个json字符串?有人有过这个方面的经验吗

非常感谢,, Brett

我不使用YAJL,但是,苹果在iOS中也使用YAJL

无论如何,库的行为是正确的:您没有创建“用户”词典

您需要执行以下操作:

NSDictionary *serialize = [NSDictionary dictionaryWithObject:userDictionary forKey:@"user"];
然后“JSON”这个


这是因为当您在userDictionary上调用
-yajl_JSONString
时,您正在使用userDictionary作为“根对象”。如果你想在另一个字典中包装它,你需要显式地这样做。

我强烈建议你检查一下JSONKit,我使用了很多JSON解析器,发现它是最简单的


文档也很棒。

好的,对。这就是我需要知道怎么做的。你知道如何显式地做到这一点吗?我告诉过你:)创建一个NSDictionary对象,就像我写的“serialize”,然后调用[serialize yajl_JSONString],而不是[userDictionary yajl_JSONString]。。。就那样!啊,我误解了。好了,现在说得很有道理!非常感谢!