0

Could somebody help me with an problem, i'm trying to parse Deet with NSserialization. I'm trying to parse the next 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"
            ]
        }
    ]
}

i'm trying to parse the 'Price' with this code:

  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];

In the NSLog i receive:

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"
)

And now i'm trying to parse each price in an tablecell i tried something like:

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

But it's crashing;

-[__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) 
Frenck
  • 6,514
  • 5
  • 22
  • 25
  • Sorry Frenck, it is really hard to understand what you want, I can see what you did, but what is the output that is you expecting ? The error is pretty obvious, you have an array when you expect a string, but you need to explain, what exactly you want to do based on the json you put there, maybe exists a easy way to do that. Also you must use variables named in lower camel case. – ggrana Aug 10 '12 at 14:47

2 Answers2

0

First off, when you create your 'scrubbedData' NSDictionary you are going to be adding two keys @"Price" keys to it which is not a good idea.

Aside from that, I'm a little unclear as to what you are actually trying to do. It seems like you want to get a single array of prices and output each of them to an individual row in a UITableView. If that is the case, I would suggest something like this:

(leave earlier code as it is) ....

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];
        }
    }
}

Then, in your UITableViewDataSource method

cell.Price.text = [dataNS objectAtIndex:[indexPath row]];
Killian
  • 414
  • 2
  • 10
-1

The objects referenced by the Price key are arrays of prices, not single prices. cell.Price.text clearly expects a string, not an array.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • Create a function which parses the elements of Price into a string, then assign this string as value for cell.Price.Text – Lajos Arpad Aug 10 '12 at 12:56
  • @Frenck You either need to concatenate the prices as Lajos says or you need to pick one of them, or you need a separate text cell for each price. – JeremyP Aug 10 '12 at 13:03
  • Nope, I'm a noob in Objective C. I know what's the solution to your problem but I don't know how you should implement that. My best quess without actually knowing the technology that you should do something like this: http://stackoverflow.com/questions/1828665/convert-nsarray-to-nsstring-in-objective-c – Lajos Arpad Aug 10 '12 at 13:23
  • @Frenck `-objectAtIndex:` will allow you to get the individual objects in the array. – JeremyP Aug 10 '12 at 14:33
  • @Frenck Also look at `for(... in ...)` – JeremyP Aug 10 '12 at 14:33