使用JsonModelparsingjson

在获取json数组时遇到问题。

我正在使用JsonModel库json handelling。

我的JSON数据是:

[ { "TSCard_id": 100, "TSCardBackend_id": "TSu1t1", "TSCard_date": "10/11/2013", "TSCard_resource_id": "", "TSCard_resource_name": "", "TSCard_job_id": "JOB_M2156 ", "TSCard_job_desc": "BAGARIA MILLIPORE - BOM ", "TSCard_activity_id": "B03", "TSCard_activity_desc": "Closing", "TSCard_hours": "4.0", "TSCard_chargeableflag": "0", "TSCard_desc": "Description:", "TSCard_syncflag": "0", "TSCard_flag": "", "user_id": "1" }, { "TSCard_id": 101, "TSCardBackend_id": "TSu1t2", "TSCard_date": "12/11/2013", "TSCard_resource_id": "", "TSCard_resource_name": "", "TSCard_job_id": "JOB_B0002", "TSCard_job_desc": "ABB LIMITED - BLR ", "TSCard_activity_id": "NB01", "TSCard_activity_desc": "Admin ", "TSCard_hours": "5.0", "TSCard_chargeableflag": "1", "TSCard_desc": "Description:", "TSCard_syncflag": "0", "TSCard_flag": "", "user_id": "1" } ] 

上面的json有2个TSCard class对象

  //TSCard.h #import <Foundation/Foundation.h> #import <JSONModel.h> @protocol TSCard @end @interface TSCard : JSONModel @property (nonatomic, retain) NSString *TSCard_id; @property (nonatomic, retain) NSString *TSCardBackend_id; @property (nonatomic, retain) NSString *TSCard_date; @property (nonatomic, retain) NSString *TSCard_resource_id; @property (nonatomic, retain) NSString *TSCard_resource_name; @property (nonatomic, retain) NSString *TSCard_job_id; @property (nonatomic, retain) NSString *TSCard_job_desc; @property (nonatomic, retain) NSString *TSCard_activity_id; @property (nonatomic, retain) NSString *TSCard_activity_desc; @property (nonatomic, retain) NSString *TSCard_hours; @property (nonatomic, retain) NSString *TSCard_chargeableflag; @property (nonatomic, retain) NSString *TSCard_desc; @property (nonatomic, retain) NSString *TSCard_syncflag; @property (nonatomic, retain) NSString *TSCard_flag; @property (nonatomic, retain) NSString *user_id; @end 

我正在为TSCardtypes的arrays做一个类

  //GetCardData.h #import <JSONModel.h> #import "TSCard.h" @interface GetCardData : JSONModel @property(strong,nonatomic)NSArray<TSCard>* myDataArray; @end 

然后我parsingjson如下:

 NSError* err = nil; GetCardData *c = [[GetCardData alloc] initWithString:jsonString error:&err]; NSLog(@"%d",c.myDataArray.count); 

NSLog错误:错误域= JSONModelErrorDomain代码= 1“无效的JSON数据:尝试初始化JSONModel对象使用initWithDictionary:错误:但字典参数不是'NSDictionary'”。 UserInfo = 0x8265490 {NSLocalizedDescription =无效的JSON数据:尝试使用initWithDictionary初始化JSONModel对象:错误:但字典参数不是'NSDictionary'。}

我无法find我错在哪里…任何人都可以build议正确的方式使用JsonModel和正确parsing一个jsonString TSCard对象数组。

尝试使用下面的代码来parsing你的json和获取数组

 NSMutableArray *yourArray = [TSCard arrayOfModelsFromDictionaries:jsonString]; 

比创build和分配

 GetCardData *objCardData = [[GetCardData alloc] init]; objCardData.myDataArray = yourArray; 

或者你需要改变你的JSONString如下

  { "myDataArray": [ { "TSCard_id": 100, "TSCardBackend_id": "TSu1t1", "TSCard_date": "10/11/2013", "TSCard_resource_id": "", "TSCard_resource_name": "", "TSCard_job_id": "JOB_M2156 ", "TSCard_job_desc": "BAGARIA MILLIPORE - BOM ", "TSCard_activity_id": "B03", "TSCard_activity_desc": "Closing", "TSCard_hours": "4.0", "TSCard_chargeableflag": "0", "TSCard_desc": "Description:", "TSCard_syncflag": "0", "TSCard_flag": "", "user_id": "1" }, { "TSCard_id": 101, "TSCardBackend_id": "TSu1t2", "TSCard_date": "12/11/2013", "TSCard_resource_id": "", "TSCard_resource_name": "", "TSCard_job_id": "JOB_B0002", "TSCard_job_desc": "ABB LIMITED - BLR ", "TSCard_activity_id": "NB01", "TSCard_activity_desc": "Admin ", "TSCard_hours": "5.0", "TSCard_chargeableflag": "1", "TSCard_desc": "Description:", "TSCard_syncflag": "0", "TSCard_flag": "", "user_id": "1" } ] } 

比你的代码肯定会工作。 告诉我是否需要更多的帮助。

对于你的问题,我个人推荐BWJSONMatcher 。 除了数据模型之外,您不必执行其他任何操作或声明任何协议:

 @interface TSCard : NSObject @property (nonatomic, strong) NSString *TSCard_id; @property (nonatomic, strong) NSString *TSCardBackend_id; @property (nonatomic, strong) NSString *TSCard_date; @property (nonatomic, strong) NSString *TSCard_resource_id; @property (nonatomic, strong) NSString *TSCard_resource_name; @property (nonatomic, strong) NSString *TSCard_job_id; @property (nonatomic, strong) NSString *TSCard_job_desc; @property (nonatomic, strong) NSString *TSCard_activity_id; @property (nonatomic, strong) NSString *TSCard_activity_desc; @property (nonatomic, strong) NSString *TSCard_hours; @property (nonatomic, strong) NSString *TSCard_chargeableflag; @property (nonatomic, strong) NSString *TSCard_desc; @property (nonatomic, strong) NSString *TSCard_syncflag; @property (nonatomic, strong) NSString *TSCard_flag; @property (nonatomic, strong) NSString *user_id; @end 

然后你可以用一条整齐的短线来排列你的数组:

 NSArray *dataArray = [BWJSONMatcher matchJSON:jsonString withClass:[TSCard class]]; 

你可以在JSONModel使用这个方法

 NSError *error; NSMutableArray <TSCard *> *yourArray = [TSCard arrayOfModelsFromString:strData error:&error]; 

并做你的逻辑。