Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.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
Iphone 使用NSSerlization解析URL编码字符串中的content_Iphone_Ios_Json - Fatal编程技术网

Iphone 使用NSSerlization解析URL编码字符串中的content

Iphone 使用NSSerlization解析URL编码字符串中的content,iphone,ios,json,Iphone,Ios,Json,我在test.html文件中有以下内容 <a href="http://playSound/?arguments=%7B%20soundID%3A%20%E2%80%9CDOG%E2%80%9D%2C%20foreground%3A%20false%2C%20loop%3A%20true%20%7D">SHOULD PLAY DOG IF YOU CLICK ME!</a> 我已经在iOS模拟器上加载了html,我想解析它并让DOG将其传递给我的函数,这样它就可以玩

我在test.html文件中有以下内容

<a href="http://playSound/?arguments=%7B%20soundID%3A%20%E2%80%9CDOG%E2%80%9D%2C%20foreground%3A%20false%2C%20loop%3A%20true%20%7D">SHOULD PLAY DOG IF YOU CLICK ME!</a>

我已经在iOS模拟器上加载了html,我想解析它并让DOG将其传递给我的函数,这样它就可以玩DOG,而无需硬编码

我该怎么做?我是objective-C新手,需要代码和编码结构方面的帮助


到目前为止,我只将html内容加载到模拟器中,我使用URL编码从JSON获取此URL。我接下来的步骤是使用NSSerlization将“DOG”取出,并对其进行解析

如果您有URL解码参数,那么您将有一个如下所示的字符串:

{ soundID: “DOG”, foreground: false, loop: true }
假设您将其加载到NSString中,您可以从中获得NSDictionary

NSString *string = @"{ soundID: “DOG”, foreground: false, loop: true }";
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
现在,您可以很容易地获得SoundID

NSString *soundID = dictionary[@"soundID"];
编辑:

要检索参数GET variable,假设您的URL如下:

NSString *url = @"<a href="http://playSound/?arguments=%7B%20soundID%3A%20%E2%80%9CDOG%E2%80%9D%2C%20foreground%3A%20false%2C%20loop%3A%20true%20%7D">SHOULD PLAY DOG IF YOU CLICK ME!</a>";

谢谢你的详细介绍,这真的有助于理解。我想知道我可以直接从我上面的URL来做吗?当然可以。您只需要检索参数。我已经更新了我的答案。
NSArray *urlComps = [url componentsSeparatedByString:@"arguments="];
NSString *arguments = [urlComps[1] componentsSeparatedByString:@"\""][0];
arguments = [arguments stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];