如何将JSONObject转换为JSONArray

时间:2014-06-30 23:54:41

标签: ios iphone json xcode xcode5

我需要在我的JSONArray中获取JSONObject。 这是我的JSON:

[
{
    "id": 9,
    "holder_rut": 22222222,
    "holder_name": "Maria Alcala",
    "rut": 22222222,
    "name": "Maria Alcala",
    "refunds_view": [
        {
            "id": 52,
            "request_number": 987654,
            "policy_code": "200000000",
            "company": "Benefits",
            "beneficiary_id": 9,
            "concept": "Prueba More Ocho",
            "date": "2013-05-05",
            "amount": 20001,
            "deductible_amount": 0,
            "max_applied": 0,
            "yearly_balance": 98,
            "payment_amount": 14001,
            "payment_method": "Deposito",
            "bank": "Estado",
            "account_number": "2345678",
            "payment_date": "2014-05-05",
            "created_at": "2014-06-18 21:48:03"
        },
        {
            "id": 99,
            "request_number": 2244665,
            "policy_code": "200000000",
            "company": "Benefits",
            "beneficiary_id": 9,
            "concept": "Prueba More Ocho",
            "date": "2014-04-29",
            "amount": 20001,
            "deductible_amount": 0,
            "max_applied": 0,
            "yearly_balance": 98,
            "payment_amount": 14001,
            "payment_method": "Deposito",
            "bank": "Estado",
            "account_number": "2345678",
            "payment_date": "2014-05-29",
            "created_at": "2014-06-27 23:21:52"
        }
    ]
}
] 

在这个JSON中,我得到两个id = 52且id = 99

的JSONObjects

我需要使用xcode将不同的对象变为退款视图

解析此JSON的代码在这里:

NSDictionary *jsonResponse = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];//I'm a Json Refunds
self.list = [jsonResponse valueForKey:@"refunds_view"];
refunds_view = [[NSMutableArray alloc] init];

for (NSDictionary *each_refunds in self.list) {
    //NSLog(@"each_refound %@",each_refunds);

    Refunds *refunds = [[Refunds alloc] initWithJSONData:each_refunds];
    [refunds_view addObject:refunds]; 
}

Refunds.h

#import <Foundation/Foundation.h>

@interface Refunds : NSObject

-(id)initWithJSONData:(NSDictionary*)data;

 @property (assign) NSNumber *refunds_id;
 @property (assign) NSNumber *request_number;
 @property (strong) NSString *policy_code;
 @property (strong) NSString *company;

 @end

Refunds.m

#import "Refunds.h"

@implementation Refunds 

@synthesize refunds_id;
@synthesize request_number;
@synthesize policy_code;
@synthesize company;
-(id)initWithJSONData:(NSArray*)data{
self = [super init];
if(self){
    NSLog(@"initWithJSONData method called ");

    refunds_id =  [data valueForKey:@"id"];
    request_number = [data valueForKey:@"request_number"];
    policy_code = [data valueForKey:@"policy_code"];
     company = [data valueForKey:@"company"];
  }
  return self;
  }
  @end

1 个答案:

答案 0 :(得分:2)

您的JSON是一个数组,但您正在提取NSDictionary。

NSArray *jsonResponse = [NSJSONSerialization 
    JSONObjectWithData:jsonStringAsNSData options:0 error:&error];
NSDictionary *jsonDictionary = jsonResponse.firstObject;
NSArray *objects = jsonDictionary[@"refunds_view"];