2010-07-23 76 views
0
 
JSON As Dictionary 
{ 
    headers =  (
       { 
      backGroundImageUrl = ""; 
      dataField = Name; 
      headerText = Name; 
      id = Name; 
      itemRenderer = ""; 
      toolTip = ""; 
      width = 60; 
     }, 
       { 
      backGroundImageUrl = ""; 
      dataField = BidPrice; 
      headerText = Bid; 
      id = BidPrice; 
      itemRenderer = ""; 
      toolTip = "Bid Price"; 
      width = 30; 
     }); 
values =  (
       { 
      assetCellValueLst =    { 
       AskColorCode = "#B8D1ED"; 
       AskPrice = "102.20"; 
       BidColorCode = "#B8D1ED"; 
       BidPrice = "102.00"; 
       Name = "AR Bonar 11"; 
       PECSAsk = 569; 
       PECSChg = "(31)"; 
       PECSChgColorCode = "#000000"; 
       PriceChg = "0.00"; 
       PriceChgColorCode = "#000000"; 
       SOLAsk = 604; 
       SSPAsk = 677; 
       SSPChgDay = "+3"; 
       SSPChgDayColorCode = "#000000"; 
       YTMAsk = "6.97"; 
       assetGroupName = Argentina; 
       assetId = ARBONAR11; 
       iconPath = "images/flag_Argentina.gif"; 
       updated = false; 
      }; 
      assetId = ARBONAR11; 
     }, 
       { 
      assetCellValueLst =    { 
       AskColorCode = "#53840f"; 
       AskPrice = "84.00"; 
       BidColorCode = "#53840f"; 
       BidPrice = "83.75"; 
       Name = "AR Bod 15"; 
       PECSAsk = 945; 
       PECSChg = 14; 
       PECSChgColorCode = "#000000"; 
       PriceChg = "-0.10"; 
       PriceChgColorCode = "#53840F"; 
       SOLAsk = 985; 
       SSPAsk = 1007; 
       SSPChgDay = "+7"; 
       SSPChgDayColorCode = "#000000"; 
       YTMAsk = "11.74"; 
       assetGroupName = Argentina; 
       assetId = ARBON15; 
       iconPath = "images/flag_Argentina.gif"; 
       updated = false; 
      }; 
      assetId = ARBON15; 
     }); 

爲清晰起見,上面的JSON有更多的標題和值縮短。 assestCellValueLst Dictionary可以具有更多或更少的鍵值對,具體取決於業務邏輯。標題字典也是如此。我應該如何在Xcode中創建對象模型以將其與Core Data Managed Objects結合使用?對於非持久性商店或根本沒有商店如何設計對象圖形/模型?

回答

0

簡單地說,您只需爲要保留的每種字典類型創建一個實體,併爲字典中的每個鍵都有一個屬性。

在這種情況下,您會看到兩個實體:HeaderAsset。如果頭部有資產一對一的關係,你可能有這樣的(僞)實體:

Header{ 
    backGroundImageUrl = string; 
    dataField = string; 
    headerText = string; 
    id = string; 
    itemRenderer = ?; 
    toolTip = ? 
    width = integer; 
    asset<--(required,cascade)-->Asset.header 
} 

Asset{ 
    assetId = string; 
    askColorCode = string; 
    askPrice = float; 
    bidColorCode = string; 
    bidPrice = float; 
    //...etc 
    header<--(required,nullify)-->Header.asset 
} 

的基本思路是,該模型,很好,「模型」無論現實世界對象,事件,條件或者你想要堅持的信息。在這種情況下,JSON提供了漂亮,整潔的字典,所有數據已經​​組織並標記給您。

+0

非常感謝你 – user400579 2010-07-25 20:04:35

0
 

+(NSArray *)managedObjectsFromJSONDictionary:(NSDictionary *)dictionary withManagedObjectContext:(NSManagedObjectContext *)moc 
{ 
    NSMutableArray *finalArrayOfArrays = [[NSMutableArray alloc] init]; 
    NSArray *allKeys = [dictionary allKeys]; 
    for(NSString *keyName in allKeys) 
    { 
     id structureArray = [dictionary valueForKey:keyName]; 
     if([structureArray isKindOfClass:[NSArray class]]) 
     { 
      NSMutableArray *objectArray = [[NSMutableArray alloc] init]; 
      for(NSDictionary *structureDictionary in (NSArray *)structureArray) 
      { 
       [objectArray addObject:[JSONtoManagedObject managedObjectFromDictionary:structureDictionary withManagedObjectContext:moc forEntity:keyName]]; 
      } 
      [finalArrayOfArrays addObject:objectArray]; 
      [objectArray release]; 
     } 
    } 
    return [finalArrayOfArrays autorelease]; 
} 
+(NSManagedObject *)managedObjectFromDictionary:(NSDictionary *)dictionary withManagedObjectContext:(NSManagedObjectContext *)moc forEntity:(NSString *)entityName 
{ 
    NSManagedObject *managedObject; 
    NSArray *arrayOfKeys = [dictionary allKeys]; 
    for(NSString *name in arrayOfKeys) 
    { 
     id obj = [dictionary objectForKey:name]; 
      managedObject = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:moc]; 
       if (![obj isKindOfClass:[NSDictionary class]]) 
       { 
        if([name isEqualToString:@"id"]) 
         [managedObject setValue:obj forKey:@"uniqueID"]; 
        else 
         [managedObject setValue:obj forKey:name]; 
       } 
       else 
       { 
        NSDictionary *relationshipDictionary = [[managedObject entity] relationshipsByName]; 
        for(NSString *relationshipName in [relationshipDictionary allKeys]) 
        { 
         NSRelationshipDescription *relationshipDescription = [relationshipDictionary objectForKey:relationshipName]; 
         if(![relationshipDescription isToMany] && [relationshipName isEqualToString:name]) 
         { 
          NSManagedObject *childObject = [JSONtoManagedObject managedObjectFromDictionary:obj withManagedObjectContext:moc forEntity:relationshipName]; 
          [managedObject setValue:childObject forKey:relationshipName]; 
          continue; 
         } 
         NSMutableSet *relationshipSet = [managedObject mutableSetValueForKey:relationshipName]; 
         NSArray *relationshipArray = [obj valueForKey:relationshipName]; 
         for(NSDictionary *insideDictoinary in relationshipArray) 
         { 
          NSManagedObject *childObject = [JSONtoManagedObject managedObjectFromDictionary:insideDictoinary withManagedObjectContext:moc forEntity:relationshipName]; 
          [relationshipSet addObject:childObject]; 
         } 
        } 
       }//end else 
     } // for 
    return managedObject; 
} 
+0

我的模型有頭作爲一個實體,和值與assestCellValueLst的實體當作一個與值一一對應關係,我得到的所有頭鍵值零除UNIQUEID,我不得不這樣做,因爲身份證被視爲一個保留字,併爲我創造了問題... – user400579 2010-07-25 20:03:48