Objective c 目标C:如何将以下JSON转换为可用数据
我有一个JSON字符串,如下所示Objective c 目标C:如何将以下JSON转换为可用数据,objective-c,json,Objective C,Json,我有一个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"]] 我也有一节课
[["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"]]
我也有一节课
例如class.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
#导入
@接口示例类:NSObject
@属性(非原子,强)NSString*Level0;
@属性(非原子,强)NSString*Level1;
@属性(非原子,强)NSString*Level2;
@属性(非原子,强)NSString*Level3;
@属性(非原子,强)NSString*Level4;
@属性(非原子,强)NSString*Level5;
@结束
显然,在JSON字符串中,数组中的第一项是ExampleClass Level0,数组中的第二项是ExampleClass Level1等
理想情况下,我希望将JSON数据转换为NSMutableArray到NSDictionary的格式。我只是不知道如何转换它
谢谢首先将字符串转换为NSData。假设NSString JSONString包含您的JSON字符串:
NSData* JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
然后将此数据转换为NSArray:
NSError *e;
NSArray* finalData = [NSJSONSerialization JSONData options:nil error:&e];
对象
finalData
将包含您的所有元素。但是请注意,从字符串看,finalData本身似乎包含2个数组。这两个数组将具有字符串NSJSONSerialization。@user3521174问题中的JSON是从哪里获得的?请注意,示例JSON不是NSString
,请注意引号字符不会转义。