2014-08-27 18 views
-1

我有一個JSON字符串類似如下的Objective-C:如何以下JSON轉換爲可用數據

[["Exampe_Level0_1","Exampe_Level1_1","Exampe_Level2_1","Exampe_Level3_1","Exampe_Level4_1","Exampe_Level5_1"],["Exampe_Level0_2","Exampe_Level1_2","Exampe_Level2_2","Exampe_Level3_2","Exampe_Level4_2","Exampe_Level5_2"]] 

我也有一類

ExampleClass.h

#import <Foundation/Foundation.h> 

@interface ExampleClass : NSObject 

@property(nonatomic, strong)NSString *Level0; 
@property(nonatomic, strong)NSString *Level1; 
@property(nonatomic, strong)NSString *Level2; 
@property(nonatomic, strong)NSString *Level3; 
@property(nonatomic, strong)NSString *Level4; 
@property(nonatomic, strong)NSString *Level5; 
@end 

顯然在JSON字符串中,數組中的第一項是ExampleClass Level0,數組中的第二項是ExampleClass Level1等。

理想情況下,我想將JSON數據轉換爲NSMutableArray轉換爲NSDictionary的格式。我只是失去了如何轉換它。

謝謝

+2

NSJSONSerialization。 – 2014-08-27 21:10:02

+0

@ user3521174你在哪裏得到了JSON的問題? – zaph 2014-08-27 21:32:45

回答

0

首先你將你的字符串轉換爲NSData。假設的NSString JSONString包含您的JSON字符串:

NSData* JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding]; 

那麼這個數據轉換成一個NSArray:

NSError *e; 
NSArray* finalData = [NSJSONSerialization JSONData options:nil error:&e]; 

的對象finalData將包含所有要素。但是請注意,看着你的字符串看起來好像finalData本身將包含2個數組。這兩個數組將有你的字符串

+1

請注意,示例JSON不是'NSString',請注意引號字符不會被轉義。 – zaph 2014-08-27 21:28:16

相關問題