本文介绍了使用JsonModel解析json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Facing problem while getting a array of 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有2个类型为TSCard class

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

我正在为类型为TSCard

 //GetCardData.h

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


@interface GetCardData : JSONModel

@property(strong,nonatomic)NSArray<TSCard>* myDataArray;

@end

然后我按如下方式解析json:

then I parsing json as below:

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

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

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

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'.}

我找不到我错了...有人可以建议使用JsonModel并正确解析jsonString到TSCard对象数组的正确方法.

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.

推荐答案

尝试使用以下代码来解析json并获取数组

Try using following code for parsing your json and getting array

NSMutableArray *yourArray = [TSCard arrayOfModelsFromDictionaries:jsonString];

比创建和分配

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

或者您需要按照以下方式更改JSONString

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.

这篇关于使用JsonModel解析json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 15:00