在获得一系列json时面临问题。
我正在使用JsonModel库来传递json。
我的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有两个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我正在为TSCard类型的数组创建一个类
//GetCardData.h
#import <JSONModel.h>
#import "TSCard.h"
@interface GetCardData : JSONModel
@property(strong,nonatomic)NSArray<TSCard>* myDataArray;
@end然后我对json进行如下解析:
NSError* err = nil;
GetCardData *c = [[GetCardData alloc] initWithString:jsonString error:&err];
NSLog(@"%d",c.myDataArray.count);NSLog Error: Error Domain=JSONModelErrorDomain Code=1“无效的JSON数据:尝试使用initWithDictionary:error初始化JSONModel对象:但是字典参数不是‘not’”。UserInfo=0x8265490 {NSLocalizedDescription=Invalid JSON数据:尝试使用initWithDictionary:error:初始化JSONModel对象:但字典参数不是‘NSDictionary’。}
我找不到wrong...can,有人建议正确地使用JsonModel和解析jsonString数组来正确地处理TSCard对象。
发布于 2013-11-28 13:26:01
尝试使用以下代码来解析json和获取数组
NSMutableArray *yourArray = [TSCard arrayOfModelsFromDictionaries:jsonString];而不是创建和分配
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"
}
]
}比你的代码更有效。如果需要更多的帮助请告诉我。
发布于 2015-11-10 07:43:35
对于您的问题,我个人推荐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]];发布于 2016-09-16 05:30:56
您可以在JSONModel中使用此方法。
NSError *error;
NSMutableArray <TSCard *> *yourArray = [TSCard arrayOfModelsFromString:strData error:&error];做你的逻辑。
https://stackoverflow.com/questions/20267152
复制相似问题