Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.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 从数组的数组解析json_Ios_Arrays_Json - Fatal编程技术网

Ios 从数组的数组解析json

Ios 从数组的数组解析json,ios,arrays,json,Ios,Arrays,Json,我的json数据位于第一个数组中,我的主类别数组名为“Response”,其中我的父类别数组名为“0”,其中我的子类别数组名为“0”,下面是我的json格式 {"Response":[{"menuname":"Jewellery","menuid":"1","0":[{"catname":"Rings","catid":"1","0":[{"scatname":"Engagement Rings","scatid":"4"},{"scatname":"Wedding Rings","scatid

我的
json
数据位于第一个数组中,我的主类别数组名为“Response”,其中我的父类别数组名为“0”,其中我的子类别数组名为“0”,下面是我的
json
格式

{"Response":[{"menuname":"Jewellery","menuid":"1","0":[{"catname":"Rings","catid":"1","0":[{"scatname":"Engagement Rings","scatid":"4"},{"scatname":"Wedding Rings","scatid":"5"},{"scatname":"kk","scatid":"35"}]},{"catname":"Pendants","catid":"2","0":[{"scatname":"Office Wear","scatid":"8"}]},{"catname":"Bracelets","catid":"3","0":[{"scatname":"Studded","scatid":"9"}]},{"catname":"Earrings","catid":"6","0":[{"scatname":"Ethnic Jhumkas","scatid":"7"}]},{"catname":"Chain's","catid":"33","0":[]},{"catname":"Jewel","catid":"34","0":[]}]},{"menuname":"Collections","menuid":"2","0":[{"catname":"SOUND OF LOVE","catid":"15","0":[{"scatname":"LOVE BRACELET","scatid":"16"}]},{"catname":"COLORFUL AFFAIR","catid":"17","0":[{"scatname":"Passion ring","scatid":"18"}]},{"catname":"Evermore Collection","catid":"19","0":[]},{"catname":"BOARDROOM GLAM ","catid":"20","0":[]},{"catname":"ETERNAL GOLD","catid":"21","0":[]},{"catname":"FASHIONISTA COLLECTION","catid":"22","0":[]}]},{"menuname":"Gold Coin","menuid":"3","0":[{"catname":"SOUND OF LOVE","catid":"15","0":[{"scatname":"LOVE BRACELET","scatid":"16"}]},{"catname":"COLORFUL AFFAIR","catid":"17","0":[{"scatname":"Passion ring","scatid":"18"}]},{"catname":"Evermore Collection","catid":"19","0":[]},{"catname":"BOARDROOM GLAM ","catid":"20","0":[]},{"catname":"ETERNAL GOLD","catid":"21","0":[]},{"catname":"FASHIONISTA COLLECTION","catid":"22","0":[]}]},{"menuname":"OFF THE SHELF","menuid":"4","0":[{"catname":"testing from pixel","catid":"13","0":[]},{"catname":"New pixel","catid":"23","0":[]},{"catname":"Evermore Collection","catid":"19","0":[]},{"catname":"BOARDROOM GLAM ","catid":"20","0":[]},{"catname":"ETERNAL GOLD","catid":"21","0":[]},{"catname":"FASHIONISTA COLLECTION","catid":"22","0":[]}]}]}
我想在可扩展的tableview中显示,如下所示

Jewellery
  Rings
     Engagement Rings
     Wedding Rings
     kk
  Pendants
     Ofice Wear
Collections
  Sound Of  Love
     Love bracelet
  Colorful Affair
     Passion ring
下面是我在
viewdidLoad

 NSDictionary *pJson;
    NSMutableString *postStr = [NSMutableString stringWithString:kURL];
  [postStr appendString:[NSString stringWithFormat:@"?tag=%@&id=%@",kCategoryFilter,kPrecious]];

    [postStr setString:[postStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:postStr]];
    NSLog(@"%@",postStr);
    [request setHTTPMethod:@"POST"];
    _connection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
    NSURL *url = [NSURL URLWithString:postStr];
    NSData *data = [NSData dataWithContentsOfURL:url];
    pJson = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSLog(@"%@",pJson);
    NSMutableArray *arr = [pJson objectForKey:@"Response"];
    NSLog(@"%lu",(unsigned long)arr.count);
    NSMutableDictionary *dict2 =[[NSMutableDictionary alloc]init];// [arr objectAtIndex:NSIndexPath.row];
   [dict2 setObject:arr forKey:@"dictionary1"];
   dict2 = [arr objectAtIndex:0];
   NSArray *arr1 =[dict2 objectForKey:@"0"];
    NSLog(@"%lu",(unsigned long)arr1.count);
    NSUInteger y;
    for (int i=0;i<arr.count;i++) {
              NSString *ring_data = [[arr objectAtIndex:i]objectForKey:@"menuname"];
        NSString *id_data = [[arr objectAtIndex:i]objectForKey:@"menuid"];
        NSLog(@"AUTHOR: %@",ring_data);
        NSLog(@"%@",id_data);
        dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                      ring_data,@"menuname",id_data,@"menuId",nil];
        [myObject addObject:dictionary];
        NSMutableArray *arr1 = [dict2 objectForKey:@"0"];
        NSLog(@"%lu",(unsigned long)arr1.count);
        y = arr.count;
        for (NSUInteger i=0;i<arr1.count;i++) {
            NSArray *count = [[[arr1 objectAtIndex:i] objectForKey:arr]valueForKey:@"catname"];
            NSString *myCount = [NSString stringWithFormat:@"%lu",(unsigned long)[count count]];
            NSString *ring_data = [[arr1 objectAtIndex:i]objectForKey:@"catname"];
            NSLog(@"AUTHOR: %@",ring_data);
            //  NSLog(@"%@",catname_data);
            dictionary1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          ring_data,@"catname",
                          nil];
            [myObject1 addObject:dictionary1];

        }      
}
NSDictionary*pJson;
NSMutableString*postStr=[nsmutablestringwithstring:kURL];
[postStr appendString:[NSString stringWithFormat:@”?标记=%@&id=%@,kCategoryFilter,kPrecious]];
[postStr设置字符串:[postStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:postStr]];
NSLog(@“%@”,postStr);
[请求设置HttpMethod:@“POST”];
_连接=[[NSURLConnection alloc]initWithRequest:请求委托:自启动中间:是];
NSURL*url=[NSURL-URLWithString:postStr];
NSData*data=[NSData dataWithContentsOfURL:url];
pJson=[NSJSONSerialization JSONObject WithData:数据选项:kNilOptions错误:nil];
NSLog(@“%@”,pJson);
NSMUTABLEARRY*arr=[pJson objectForKey:@“响应”];
NSLog(@“%lu”,(无符号长)arr.count);
NSMutableDictionary*dict2=[[NSMutableDictionary alloc]init];//[arr objectAtIndex:nsindepath.row];
[dict2 setObject:arr forKey:@“dictionar1”];
dict2=[arr objectAtIndex:0];
NSArray*arr1=[dict2 objectForKey:@“0”];
NSLog(@“%lu”,(无符号长)arr1.count);
NSUY整数;

对于(inti=0;i我为您尝试了一些编码。遵循这些代码并自定义要添加到数组的位置和设置字典的位置

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];

NSArray *array=[jsonArray objectForKey:@"Response"];

for (int i=0;i<[array count];i++)
{
    NSMutableDictionary *dict = [array objectAtIndex:i];
    NSMutableArray *arrayDictValue = [dict valueForKey:[NSString stringWithFormat:@"%d",0]];
    NSString *strMenuName = [NSString stringWithFormat:@"%@",[arrayDictValue valueForKey:@"menuname"]];
    NSString *strMenuID = [NSString stringWithFormat:@"%@",[arrayDictValue valueForKey:@"menuid"]];
    NSLog(@"The strMenuName is-%@",strMenuName);
    NSLog(@"The strMenuID is-%@",strMenuID);
    for (int j=0; j<[arrayDictValue count]; i++)
    {
        NSMutableDictionary *dictInside = [arrayDictValue objectAtIndex:j];
        NSArray *arrayInsideDict = [dictInside valueForKey:@"0"];
        for (int k =0; k<[arrayInsideDict count]; i++)
        {

            NSString *strCatName = [NSString stringWithFormat:@"%@",[[arrayInsideDict objectAtIndex:k ]valueForKey:@"catname"]];
            NSString *strCatID = [NSString stringWithFormat:@"%@",[[arrayInsideDict objectAtIndex:k ]valueForKey:@"catid"]];
            NSLog(@"The strCateName is-%@",strCatName);
            NSLog(@"The strCatID is-%@",strCatID);
            NSMutableDictionary *dictInArray = [[arrayInsideDict objectAtIndex:0] valueForKey:@"0"];
            NSString *strSCatName = [NSString stringWithFormat:@"%@",[dictInArray valueForKey:@"scatname"]];
            NSString *strSCatID = [NSString stringWithFormat:@"%@",[dictInArray valueForKey:@"scatid"]];
            NSLog(@"the strSCatName is - %@",strSCatName);
            NSLog(@"the strSCatID is - %@",strSCatID);

        }


    }


}
NSDictionary*jsonArray=[NSJSONSerialization JSONObjectWithData:responseData选项:NSJSONReadingMutableContainers错误:&err];
NSArray*数组=[jsonArray objectForKey:@“Response”];

对于(int i=0;iLop是无限的,strCatName是(null)您检查得很好吗?首先在该dictionside之后打印arrayDictValue,arrayInsideDict.Check清楚。如果它没有任何对象,请快速告诉我。是的,它们有对象。那么您在哪里没有对象?