parse json using JsonModel

杀马特。学长 韩版系。学妹 提交于 2019-12-07 17:13:48

问题


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

And I am making a class for array of type TSCard

 //GetCardData.h

#import <JSONModel.h>
#import "TSCard.h"


@interface GetCardData : JSONModel

@property(strong,nonatomic)NSArray<TSCard>* 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.


回答1:


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.




回答2:


For your problem, I personally recommend BWJSONMatcher. You don't have to do anything else or declare any protocol apart from your data model:

@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

You can then get your array with one neatly short line:

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



回答3:


You can using this method in JSONModel

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

and do your logic.



来源:https://stackoverflow.com/questions/20267152/parse-json-using-jsonmodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!