-4

我正在从 Web 服务获取数据,我将其放入NSDictionary,并通过使用valueForKey方法,我将数据绑定到一个数组中,它有 17 个元素,但计数显示为 1,因为第一个数据有双引号。这是示例代码:

- (void)BarChartfetchedData:(NSData *)responseData   {

    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    BarChartDictionary = [json   objectForKey:@"Report_DashboardDetailProfitBranchResult"];
    NSArray * new = [[NSArray alloc]initWithObjects:[BarChartDictionary valueForKey:@"ProfitAmount"],nil];

    NSLog(@"mypieclass.itemArray: %@ ",new);
    NSLog(@"Total: %lu ",(unsigned long)[new count]);
    NSLog(@"BarChartDictionary: %lu ",(unsigned long)[BarChartDictionary count]);
}

需要获得总数 17。如何做到这一点,任何建议都会有所帮助。

4

2 回答 2

2

尝试

NSArray * new = [[NSArray alloc]initWithArray:[BarChartDictionary valueForKey:@"ProfitAmount"]]; // if not using ARC you will have to release it.

要不就

NSArray *new = BarChartDictionary[@"ProfitAmount"];

芝麻街手偶解说:

(  // <- begin top level array
    ( // <- begin first element in top level array. it is another array, a nested array
        "16291443.69", // <- first element in the nested array
         6621797,      // <- second element in the nested array
         5692671, 
         2477348, 
         2362607, 
         2281261, 
         886410, 
         848799, 
         762441, 
         706688, 
         497076, 
         492402, 
         188320, 
         124595, 
         96625, 
         62905, 
         60200       // <- last element in the nested array
    ) // <- end first element in top level array
) // <- end top level array

--> 顶层数组只有一个元素。那是另一个有 17 个元素的数组。

于 2013-07-15T13:46:02.157 回答
0

字典对象是否正确打印了 17 值?如果字典本身是正确的,您可能需要对字典中的计数对象进行不同的解析。

可能是您打印的 count 格式错误,因为它实际上不是 long unsigned...因为您将其转换为 long unsigned,编译器将不会显示警告...

于 2013-07-15T13:34:25.320 回答