2012-08-10 86 views
0

有人可以幫我解決問題,我試圖用NSserialization來解析Deet。我試圖解析下一個JSON:解析屬性NSserialization

{ 
    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" 
      ] 
     } 
    ] 
} 

我試圖解析與此代碼 '價格':

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

現在我試圖解析表格中的每個價格我試過類似:

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) 
+0

對不起Frenck,真的很難理解你想要什麼,我可以看到你做了什麼,但是你期望的輸出是什麼?這個錯誤非常明顯,當你需要一個字符串時,你有一個數組,但是你需要解釋一下,基於你放在那裏的json,你想要做什麼,也許存在一個簡單的方法來做到這一點。您也必須使用以較低駱駝大小寫命名的變量。 – ggrana 2012-08-10 14:47:59

回答

0

首先,當您創建'scrubbedData'NSDictionary時,您將要添加兩個鍵@「價格」鍵給它,這不是一個好主意。

除此之外,我還有點不清楚你到底在做什麼。看起來你想要得到一個單一的價格數組,並將它們中的每一個輸出到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

Price key引用的對象是價格數組,而不是單個價格數組。 cell.Price.text顯然需要一個字符串,而不是一個數組。

+0

我該如何解決這個問題? – Frenck 2012-08-10 12:50:43

+0

創建一個函數,將Price的元素解析爲一個字符串,然後將此字符串作爲cell.Price.Text的值賦值 – 2012-08-10 12:56:06

+0

@Frenck您可能需要按照Lajos的說法連接價格,或者您需要選擇其中一個,或者您每個價格需要一個單獨的文本單元格。 – JeremyP 2012-08-10 13:03:51