java读取1t json文件,使用JsonModel解析json

Facing problem while getting a array of json.

I am using JsonModel library for json handelling.

My json data is:

[

{

"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"

}

]

Above json has 2 objects of type TSCard class

//TSCard.h

#import

#import

@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

And I am making a class for array of type TSCard

//GetCardData.h

#import

#import "TSCard.h"

@interface GetCardData : JSONModel

@property(strong,nonatomic)NSArray* myDataArray;

@end

then I parsing json as below:

NSError* err = nil;

GetCardData *c = [[GetCardData alloc] initWithString:jsonString error:&err];

NSLog(@"%d",c.myDataArray.count);

NSLog Error:

Error Domain=JSONModelErrorDomain Code=1 "Invalid JSON data: Attempt to initialize JSONModel object using initWithDictionary:error: but the dictionary parameter was not an 'NSDictionary'." UserInfo=0x8265490 {NSLocalizedDescription=Invalid JSON data: Attempt to initialize JSONModel object using initWithDictionary:error: but the dictionary parameter was not an 'NSDictionary'.}

I can't find where I am wrong...can anybody suggest the right way of using JsonModel and parsing a jsonString to array of TSCard objects correctly.

解决方案

Try using following code for parsing your json and getting array

NSMutableArray *yourArray = [TSCard arrayOfModelsFromDictionaries:jsonString];

Than create and assign

GetCardData *objCardData = [[GetCardData alloc] init];

objCardData.myDataArray = yourArray;

or you need to change your JSONString as follow

{

"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"

}

]

}

Than your code will definitely work. Tell me if need more help.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值