Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.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
Php 如何在Objective C中解析嵌套的JSON数组_Php_Objective C_Arrays_Json_Parsing - Fatal编程技术网

Php 如何在Objective C中解析嵌套的JSON数组

Php 如何在Objective C中解析嵌套的JSON数组,php,objective-c,arrays,json,parsing,Php,Objective C,Arrays,Json,Parsing,我有JSON树,我需要按类别键对数组进行分组。(在本例中,:“”、“1”、“2”、“3”和“serf”是类别的值名称。)我正在使用NSJSONSerialization序列化JSON,但无法按我想要的方式解析它。以下是我的一些代码: NSData *data2=[NSData dataWithContentsOfURL:url2]; result2=[NSJSONSerialization JSONObjectWithData: data2

我有JSON树,我需要按类别键对数组进行分组。(在本例中,:“”、“1”、“2”、“3”和“serf”是类别的值名称。)我正在使用NSJSONSerialization序列化JSON,但无法按我想要的方式解析它。以下是我的一些代码:

NSData *data2=[NSData dataWithContentsOfURL:url2];
result2=[NSJSONSerialization JSONObjectWithData: data2
                                        options: NSJSONReadingMutableContainers
                                          error: nil];
NSLog(@"button data: %@", result2);
for (NSDictionary *strh in [result2 objectForKey:@"template"]) {
    //the json struct above is called result2 here
    //now i need to parse any category with their names..           
}  

相关:是我关于如何在PHP中创建具有键值匹配的多维数组的问题。

查看输出,您有一个字典,其中有一个名为
模板的键和一个名为
版本的键。键
模板的对象本身就是一个包含类别键的字典。以下代码:

id foo = [[result2 objectForKey: @"template"] objectForKey: @""];
foo
设置为包含id为52、19、2、22的项目的数组

类别1、2、3更为棘手,因为键是数字还是字符串并不明显。它们可能是字符串,因为JSON中的键必须是字符串,所以

id bar = [[result2 objectForKey: @"template"] objectForKey: @"1"];

bar
设置为包含id为28、1、25的项目的数组。

实际问题是什么?“但不能以我想要的方式解析它”--你想要的结果是什么,你得到的结果是什么?@Felixyz我想要的结果是通过名称访问对象。例如,要从分类中读取按钮数据而不查看result2的结构,很难为您提供帮助。你说的这些类别是什么?它是。我已经分享了问题开头的链接,请看一下:我对命名有点困惑(我自己写的是PHP端),但我可以更改。我试过这样的事,但没有结果。