2013-05-08 46 views
-6

JSON對象有一個JSON,如: -我如何以檢索在的NSString

{  
    author =  { 
      class = "pocketapptest.Author"; 
      dateCreated = "2013-05-08T00:03:46Z"; 
      firstName = Kurt; 
      id = 1; 
      lastName = Vonnegut; 
      lastUpdated = "2013-05-08T00:03:46Z"; 
     }; 
     class = "pocketapptest.Book"; 
     dateCreated = "2013-05-08T00:03:46Z"; 
     id = 1; 
     lastUpdated = "2013-05-08T00:03:46Z"; 
     title = "Slaughterhouse Five"; 
    } 

而且我可以訪問dateceated /標題

NSString* title = [jsondata objectForKey:@"title"]; 

但我怎麼能存儲的firstName/lastName的等等。

+3

你的json無效 – 2013-05-08 09:29:44

+3

此外,解析JSON非常簡單,並且在這個站點上已經有數百次的問題了。我們不需要關於如何解析JSON的另一個問題。 – borrrden 2013-05-08 09:32:29

+0

你可以通過:jsondata [@「title」]或者jsondata [@「author」] [@「firstname」]直接訪問該屬性。 – ltebean 2013-05-08 09:33:22

回答

0

嘗試這樣,你可以得到價值像下面

NSString* firstName = [[jsondata objectForKey:@"author"]objectForKey:@"firstName"]; 
+0

謝謝:)它的工作 – Sulabh 2013-05-08 09:30:23

+0

@Sulabh:歡迎並接受答案。 – Balu 2013-05-08 09:30:49

+0

@ Sulabh:只需點擊正確的標記即可接受不需要的任何操作。 – Balu 2013-05-08 09:37:49

0

我猜你可能只使用jsondata objectForKey:兩次。就這樣:

NSString* firstName = [[jsondata objectForKey:@"author"]objectForKey:@"firstName"]; 

希望它可以幫助

0

爲mydetails

MyDetails *mydetails = [MyDetails alloc]init]; 

mydetails.class = [[jsondata objectForKey:@"author"]objectForKey:@"class"]; 
mydetails.dateCreated = [[jsondata objectForKey:@"author"]objectForKey:@"dateCreated"]; 
mydetails.firstName = [[jsondata objectForKey:@"author"]objectForKey:@"firstName"]; 
mydetails.ID = [[jsondata objectForKey:@"author"]objectForKey:@"id"]; 
mydetails.lastName = [[jsondata objectForKey:@"author"]objectForKey:@"lastName"]; 
mydetails.lastUpdated = [[jsondata objectForKey:@"author"]objectForKey:@"lastUpdated"]; 
+0

hey popeye, 訪問它們,這種方法看起來不錯。 ,所以我有10個JSON數組,我可以存儲他們,並訪問它,如果如何 – Sulabh 2013-05-08 09:46:46

+0

NSMutableArray * myDetailsArray = [NSMutableArray alloc] init];對於(){ – jailani 2013-05-08 09:51:46

+0

創建一個mutablearray爲每個對象分配MyDetails在jsonarray填充myDetail並添加到可變數組然後分解myDetail ....在循環中執行此操作 – jailani 2013-05-08 09:58:07

0

JSON是創建具有高德一類將數據從JSON存儲

@interface MyDetails 

@property(nonatomic, retain) NSString *class, *dateCreated, *firstName,*ID, *lastName, *lastUpdated; 
@end 

@implementation MyDetails 

@synthesize class, dateCreated, firstName, ID, lastName, lastUpdated; 

@end 

分配情況無效。首先使用有效的JSON。您可以檢查您的JSON是否有效here

然後從json訪問字符串對象。

希望它可以幫助你。

+0

爲什麼它無效? – Sulabh 2013-05-08 09:44:49

+0

在任何JSON驗證工具中檢查您的json – 2013-05-08 10:05:45