使用JsonModel解析json

时间:2013-11-28 13:11:55

标签: ios iphone jsonmodel

获取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

我正在为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 Domain = JSONModelErrorDomain Code = 1“无效的JSON数据:尝试使用initWithDictionary初始化JSONModel对象:错误:但字典参数不是'NSDictionary'。” UserInfo = 0x8265490 {NSLocalizedDescription =无效的JSON数据:尝试使用initWithDictionary初始化JSONModel对象:错误:但字典参数不是'NSDictionary'。}

我无法找到错误的地方......任何人都可以建议正确使用JsonModel并正确解析jsonString到TSCard个对象的数组。

3 个答案:

答案 0 :(得分:5)

尝试使用以下代码解析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"
        }
    ]
}

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

答案 1 :(得分:0)

对于您的问题,我个人推荐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]];

答案 2 :(得分:0)

您可以在JSONModel

中使用此方法
NSError *error;
NSMutableArray <TSCard *> *yourArray = [TSCard arrayOfModelsFromString:strData error:&error];

并按照你的逻辑行事。