解析属性NSserialization

时间:2012-08-10 12:29:09

标签: objective-c xcode cocoa-touch ios5

有人可以帮助我解决问题,我正在尝试使用NSserialization解析Deet。我正在尝试解析下一个JSON:

   {
    thing: "10",
    Product: [
        {
            @atri: {
                Name: "Single"
            },
            Price: [
                "21,40",

                "27,50"
            ]
        },
        {
            @atri: {
                Name: "Day"
            },
            Price: [
                "4,80",
                "3,80",
                "333,20",
                "436,60",
                "5,533553"
            ]
        }
    ]
}

我正试图用这段代码解析'Price':

  Deeta *NSApiDeet = [Deeta DeetWithContentsOfURL:[NSURL URLWithString:Planner]];
    NSError *parseError = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithDeet:NSApiDeet options:NSJSONReadingAllowFragments error:&parseError];

    NSMutableArray *DeetNS = [[NSMutableArray alloc] init];

    if ([jsonObject respondsToSelector:@selector(objectForKey:)]) {
        for (NSDictionary *Deet in [jsonObject objectForKey:@"Product"]) {
            NSDictionary *scrubbedDeet = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [Deet objectForKey:@"Price"], @"Price",

                                          nil];




            [self setTableDeet:[jsonObject objectForKey:@"Product"]];

            [DeetNS addObject:scrubbedDeet];

在我收到的NSLog中:

1: (
    "2,40",
    "1,90",
    "1,40",
    "4,10",
    "3,30",
    "2,50"
)
2012-08-10 14:26:10.242 [19716:707] 1: (
    "4,80",
    "3,80",
    "2,80",
    "8,20",
    "6,60",
    "5,00"
)

现在我正试图解析一个tablecell中的每个价格,我尝试了类似的东西:

NSDictionary *Deet = [tableDeet objectAtIndex:[indexPath row]];
  cell.Price.text = [Deet objectForKey:@"Price"];

但它崩溃了;

-[__NSCFArray isEqualToString:]: unrecognized selector sent to instance 0x2b8fc0
2012-08-10 14:28:40.835 Untitled[19734:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray isEqualToString:]: unrecognized selector sent to instance 0x2b8fc0'
*** First throw call stack:
(0x36f2a88f 0x33341259 0x36f2da9b 0x36f2c915 0x36e87650 0x350ff3f9 0xfdc89 0x3515befb 0x3515afd9 0x3515a763 0x350fef37 0x36e891fb 0x3259caa5 0x3259c6bd 0x325a0843 0x325a057f 0x325984b9 0x36efeb1b 0x36efcd57 0x36efd0b1 0x36e804a5 0x36e8036d 0x35047439 0x35129cd5 0xfa67d 0xfa618)
terminate called throwing an exception(lldb) 

2 个答案:

答案 0 :(得分:0)

首先,当您创建'scrubbedData'NSDictionary时,您将在其中添加两个键@“Price”键,这不是一个好主意。

除此之外,我有点不清楚你究竟想做什么。看起来你想获得一个单一的价格数组,并将它们中的每一个输出到UITableView中的单个行。 如果是这种情况,我会建议这样的事情:

(保留早期代码) ....

NSMutableArray *dataNS = [[NSMutableArray alloc] init];

if ([jsonObject respondsToSelector:@selector(objectForKey:)])
{
    for (NSDictionary *dataItem in [jsonObject objectForKey:@"Product"])
    {
        NSArray *priceArray = [dataItem objectForKey:@"Price"];
        for(NSString *individualPriceString in priceArray)
        {
            [dataNS addObject:individualPriceString];
        }
    }
}

然后,在你的UITableViewDataSource方法

cell.Price.text = [dataNS objectAtIndex:[indexPath row]];

答案 1 :(得分:-1)

Price键引用的对象是价格数组,而不是单个价格。 cell.Price.text显然需要一个字符串,而不是一个数组。