문제

이 JSON을 모델로 개종하려고합니다 :

  [
    {
        "status": "Top-up done",
        "amount": "25.00",
        "amount_ex_vat": "20.66",
        "executed_on": "2014-07-28 20:21:33",
        "method": "Bancontact/Mister Cash",
        "payment_received_on": "2014-07-28 20:21:29"
    },
    {
        "status": "Top-up done",
        "amount": "15.00",
        "amount_ex_vat": "12.40",
        "executed_on": "2014-05-26 19:41:36",
        "method": "PayPal",
        "payment_received_on": "2014-05-26 19:41:31"
    },
    {
        "status": "Top-up done",
        "amount": "10.00",
        "amount_ex_vat": "8.26",
        "executed_on": "2014-02-26 20:43:39",
        "method": "PayPal",
        "payment_received_on": "2014-02-26 20:43:35"
    },
    {
        "status": "Top-up done",
        "amount": "15.00",
        "amount_ex_vat": "12.40",
        "executed_on": "2014-01-18 12:43:09",
        "method": "PayPal",
        "payment_received_on": "2014-01-18 12:43:06"
    }
]
.

이것은 내 수업입니다 :

@interface TopupHistory : MTLModel 
@property (strong, nonatomic) NSString * amount;
@property (strong, nonatomic) NSString * executed_on;
@property (strong, nonatomic) NSString * method;
@end

@implementation TopupHistory

(NSDictionary *)JSONKeyPathsByPropertyKey { return @{ @"amount": @"amount", @"executed_on": @"executed_on", @"method": @"method", }; } @end
.

JSONKEYPATH를 구현할 필요가 없으므로 VARS를 정확히 똑같이 지정하지 않으면 i를 생각했습니다.

은 변환하는 내 방법입니다 :

-(void) convertFrom:(id) responseObject toModel:(Class) model resultBlock: (NetworkBlock)resultBlock
{
NSError * didFail;
id result = [MTLJSONAdapter modelOfClass:model fromJSONDictionary:responseObject error:&didFail];
if (!didFail) {
resultBlock(result,nil);
}
else
{
NSLog(@"Couldn't convert app infos JSON to model: %@", didFail);
resultBlock(nil,didFail);
}
}
.

모델이 내 클래스 이름이고 응답 봇은 AFNetworking으로 채워진 ID 오브젝트 일뿐입니다.

항상이 오류가 발생했습니다 :

Domain=MTLJSONAdapterErrorDomain Code=3 "Missing JSON dictionary" UserInfo=0x913a690 {NSLocalizedFailureReason=TopupHistory could not be created because an invalid JSON dictionary was provided: __NSCFArray, NSLocalizedDescription=Missing JSON dictionary}
2014-07-29 13:11:55.780 test Info[10953:60b] Missing JSON dictionary
.

도움이 되었습니까?

해결책

응답은 사전의 배열이므로 MTLJSONADAPTER 메소드 MODELSOFCLASS : fromJsonArray : 오류 : 맨틀 모델의 배열로 변환 할 수 있습니다.

코드는 아래와 같이 변경해야합니다 :

-(void) convertFrom:(id) responseObject toModel:(Class) model resultBlock: (NetworkBlock)resultBlock
{
NSError * didFail;
id result = [MTLJSONAdapter modelsOfClass:model fromJSONArray:responseObject error:&didFail];
//...
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top