Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.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 7中数组中json解析数据存储_Ios_Iphone_Json_Nsarray - Fatal编程技术网

ios 7中数组中json解析数据存储

ios 7中数组中json解析数据存储,ios,iphone,json,nsarray,Ios,Iphone,Json,Nsarray,这是我从API得到的响应 { Result = { Area = ( "Shah-e-Alam", Vastrapur, "Nava Vadaj", Ambavadi, "Elis Bridge", Ranip, Gota, ); }; Status = Success; } 现在我只需要在阵列中存储7个区域名称如何存储它? 我的密码在

这是我从API得到的响应

 {
Result =     {
    Area =         (

        "Shah-e-Alam",
        Vastrapur,
        "Nava Vadaj",
        Ambavadi,
        "Elis Bridge",
        Ranip,
        Gota,

    );
};
Status = Success;
 }
现在我只需要在阵列中存储7个区域名称如何存储它? 我的密码在这里

      if ([[res valueForKey:@"Status"]isEqualToString:@"Success"]) {
         NSLog(@"%@",res);
         arrAreaname = [[res valueForKey:@"Result"] mutableCopy];

           arrarea=[[arrAreaname valueForKey:@"Area"]mutableCopy];
                 NSLog(@"arrarea: %@", arrarea);

          NSData *json = [NSJSONSerialization dataWithJSONObject:arrarea      
             options:NSJSONWritingPrettyPrinted error:nil];
        NSLog(@"JSON: %@", json);

            NSString *jsonString = [[NSString alloc] initWithData:json 
             encoding:NSUTF8StringEncoding];

       NSLog(@"JSON string: %@", jsonString);



   }else{
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"ERROR"
        message:@"connection error"
            delegate:nil
            cancelButtonTitle:@"OK"
        otherButtonTitles:nil];

    [message show];
  }

我现在开始讨论如何在数组中存储数据?

我假设您的
arrAreaname
NSArray
。仔细查看您的数据:

Result =     {
    Area =         (

        "Shah-e-Alam",
        Vastrapur,
        "Nava Vadaj",
        Ambavadi,
        "Elis Bridge",
        Ranip,
        Gota,

    );
};
结果的值是一个
NSDictionary
,键区域的值是
NSArray

编辑:

您应该这样做:

arrarea= [NSArray arrayWithArray: res[@"Result"][@"Area"]];

这是示例逻辑,这将帮助您

NSArray *are=[[NSArray alloc] initWithObjects:@"Shah-e-Alam",
                  @"Vastrapur",
                  @"Nava Vadaj",
                  @"Ambavadi",
                  @"Elis Bridge",
                  @"Ranip",
                  @"Gota", nil];
    NSDictionary *dic2=[[NSDictionary alloc]initWithObjectsAndKeys:are,@"area", nil];
    NSMutableDictionary *dic1=[[NSMutableDictionary alloc]initWithObjectsAndKeys:dic2,@"Result", nil];
    NSLog(@"dic1=%@",dic1);

    NSArray *array=[[NSArray alloc]init];

    array= [NSArray arrayWithArray:[NSArray arrayWithArray:[[NSMutableDictionary dictionaryWithDictionary:[dic1 valueForKey:@"Result"]] valueForKey:@"area"]]];

     NSLog(@"array=%@",array);

您正在将字典写入json数据,而不是读取

// Use following lines to read JSON response.

NSDictionary *dictResult = [NSJSONSerialization JSONObjectWithData:<json data> options:NSJSONReadingAllowFragments error:nil];
NSArray *areas = [NSArray arrayWithArray:dictResult[@"Result"][@"Area"]];
NSLog(@"Areas : %@ %@",dictResult[@"Result"][@"Area"],areas);
//使用以下行读取JSON响应。
NSDictionary*dictResult=[NSJSONSerialization JSONObjectWithData:options:NSJSONReadingAllowFragments错误:nil];
NSArray*区域=[NSArray阵列WithArray:dictResult[@“结果”][@“区域”];
NSLog(@“区域:%@%@”,dictResult[@“结果”][@“区域”],区域);

您可以在字典中添加数据,然后在数组中添加该字典,如NSArray*数组=[[NSArray alloc]initWithArray:[dic objectForKey:@“Result”];使用类似NSArray*数组的数组=[[NSArray alloc]initWithArray:[dic objectForKey:@“Result”]valueforKey:@“Area”]您已经将您的购物车放在您的JSON马之前了。如果上面的NSLog输出是
res
,那么它已经被NSJSONSerialization解析过了,您不需要再次解析。您所称的
arrarea
是您的最终数组。这是NSLog(@“%@”,res)的输出;dic1={Result={area={area=(“Shah-e-Alam”,Vastrapur,“Nava Vadaj”,Ambavadi,“Elis Bridge”,Ranip,Gota,);};};};};}在它的停止数组参数之后不是NSArray的JSON字符串:[“Shah-e-Alam”,“Vastrapur”,“Nava Vadaj”,“Ambavadi”,“Elis Bridge”,“Ranip”,“Gota”,我需要数组中的相同结果format@UjeshPatel你们有并没有试过这个片段?把JSON响应数据放在日志中,它会给你数组,你可以把它存储在任何你想存储的地方。@UjeshPatel我已经修改了我的答案,现在它也将区域存储在数组和日志中。快乐的编码:)结果就是它自己。这里的数组显示将不同。您可以循环阵列中的所有区域。操作系统将输出存储为所需格式,您无法修改其表示形式。然后,您可能在解析之前填充了数据选择器,或者您的数组在那里无法访问。将数组设置为类实例。