2015-09-04 270 views
3

我的JSON的网址是http://api.kivaws.org/v1/loans/search.json?status=fundraisingJSON的Objective-C - 对象和数组结合

我Objective-代码显示PAGE_SIZE:20使用下面的代码

NSData *allCourseData=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]]; 



NSError *error; 
NSMutableDictionary *JsonObject = [NSJSONSerialization 
            JSONObjectWithData:allCourseData options:NSJSONReadingMutableContainers 
            error:&error]; 
//NSLog(@"json object is: - %@", JsonObject); 
//Print Page size 
NSLog(@"Page size is %@", [[JsonObject objectForKey:@"paging"] objectForKey:@"page_size"]); 

我的问题是我如何循环以Loans数组显示以下内容 示例它将显示名称,显示所有语言,状态以便输出看起来像
====================== === ===
名称: “玛丽亚路易莎”
语言:ES,EN
状态: “筹款”
部门: “零售”
位置

COUNTRY_CODE: “NI”


名称:“Florence” 语言:en
状态:“筹款”
部门:“零售”
位置
COUNTRY_CODE: “KE”

enter image description here

MyJSON饲料

{ 
    "paging": { 
     "page": 1, 
     "total": 7279, 
     "page_size": 20, 
     "pages": 364 
    }, 
    "loans": [ 
     { 
      "id": 941933, 
      "name": "Maria Luisa", 
      "description": { 
       "languages": [ 
        "es", 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 0, 
      "basket_amount": 0, 
      "image": { 
       "id": 1969857, 
       "template_id": 1 
      }, 
      "activity": "General Store", 
      "sector": "Retail", 
      "use": "to buy rice, chicken, sausages, corn, beans, and sugar.", 
      "location": { 
       "country_code": "NI", 
       "country": "Nicaragua", 
       "town": "Esteli", 
       "geo": { 
        "level": "town", 
        "pairs": "13.166667 -86.333333", 
        "type": "point" 
       } 
      }, 
      "partner_id": 176, 
      "posted_date": "2015-09-04T09:20:02Z", 
      "planned_expiration_date": "2015-10-04T09:20:02Z", 
      "loan_amount": 600, 
      "borrower_count": 1, 
      "lender_count": 0, 
      "bonus_credit_eligibility": true, 
      "tags": [] 
     }, 
     { 
      "id": 942181, 
      "name": "Florence", 
      "description": { 
       "languages": [ 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 0, 
      "basket_amount": 0, 
      "image": { 
       "id": 1970181, 
       "template_id": 1 
      }, 
      "activity": "Retail", 
      "sector": "Retail", 
      "use": "to buy more stocks of maize.", 
      "location": { 
       "country_code": "KE", 
       "country": "Kenya", 
       "town": "Bondo", 
       "geo": { 
        "level": "town", 
        "pairs": "1 38", 
        "type": "point" 
       } 
      }, 
      "partner_id": 138, 
      "posted_date": "2015-09-04T09:10:08Z", 
      "planned_expiration_date": "2015-10-04T09:10:08Z", 
      "loan_amount": 200, 
      "borrower_count": 1, 
      "lender_count": 0, 
      "bonus_credit_eligibility": true, 
      "tags": [] 
     }, 
     { 
      "id": 942146, 
      "name": "Naziha", 
      "description": { 
       "languages": [ 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 0, 
      "basket_amount": 0, 
      "image": { 
       "id": 1970129, 
       "template_id": 1 
      }, 
      "activity": "Fruits & Vegetables", 
      "sector": "Food", 
      "use": "to increase her merchandise and diversify the products in her shop.", 
      "location": { 
       "country_code": "LB", 
       "country": "Lebanon", 
       "town": "Ain Al Helwi", 
       "geo": { 
        "level": "town", 
        "pairs": "33.833333 35.833333", 
        "type": "point" 
       } 
      }, 
      "partner_id": 77, 
      "posted_date": "2015-09-04T09:10:02Z", 
      "planned_expiration_date": "2015-10-04T09:10:02Z", 
      "loan_amount": 2500, 
      "borrower_count": 1, 
      "lender_count": 0, 
      "bonus_credit_eligibility": true, 
      "tags": [] 
     }, 
     { 
      "id": 942180, 
      "name": "Sokean's Group", 
      "description": { 
       "languages": [ 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 0, 
      "basket_amount": 0, 
      "image": { 
       "id": 1970180, 
       "template_id": 1 
      }, 
      "activity": "Home Appliances", 
      "sector": "Personal Use", 
      "themes": [ 
       "Water and Sanitation" 
      ], 
      "use": "to buy a water filter to provide safe drinking water for her family.", 
      "location": { 
       "country_code": "KH", 
       "country": "Cambodia", 
       "town": "Kandal", 
       "geo": { 
        "level": "town", 
        "pairs": "13 105", 
        "type": "point" 
       } 
      }, 
      "partner_id": 311, 
      "posted_date": "2015-09-04T09:00:05Z", 
      "planned_expiration_date": "2015-10-04T09:00:05Z", 
      "loan_amount": 100, 
      "borrower_count": 2, 
      "lender_count": 0, 
      "bonus_credit_eligibility": false, 
      "tags": [ 
       { 
        "name": "volunteer_like" 
       } 
      ] 
     }, 
     { 
      "id": 941931, 
      "name": "Lucia Ingrid", 
      "description": { 
       "languages": [ 
        "es", 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 0, 
      "basket_amount": 0, 
      "image": { 
       "id": 1969852, 
       "template_id": 1 
      }, 
      "activity": "Personal Housing Expenses", 
      "sector": "Housing", 
      "themes": [ 
       "Conflict Zones" 
      ], 
      "use": "to invest in: electric wires, a fuse box, sockets, and labor to install electricity in her home.", 
      "location": { 
       "country_code": "SV", 
       "country": "El Salvador", 
       "geo": { 
        "level": "country", 
        "pairs": "13.833333 -88.916667", 
        "type": "point" 
       } 
      }, 
      "partner_id": 81, 
      "posted_date": "2015-09-04T09:00:03Z", 
      "planned_expiration_date": "2015-10-04T09:00:03Z", 
      "loan_amount": 500, 
      "borrower_count": 1, 
      "lender_count": 0, 
      "bonus_credit_eligibility": true, 
      "tags": [] 
     }, 
     { 
      "id": 941930, 
      "name": "La Manzanita Group", 
      "description": { 
       "languages": [ 
        "es", 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 0, 
      "basket_amount": 0, 
      "image": { 
       "id": 1969849, 
       "template_id": 1 
      }, 
      "activity": "Fruits & Vegetables", 
      "sector": "Food", 
      "use": "to invest in buying papayas and nance berries in bulk.", 
      "location": { 
       "country_code": "NI", 
       "country": "Nicaragua", 
       "town": "Masaya", 
       "geo": { 
        "level": "town", 
        "pairs": "11.966667 -86.1", 
        "type": "point" 
       } 
      }, 
      "partner_id": 120, 
      "posted_date": "2015-09-04T08:50:02Z", 
      "planned_expiration_date": "2015-10-04T08:50:02Z", 
      "loan_amount": 450, 
      "borrower_count": 3, 
      "lender_count": 0, 
      "bonus_credit_eligibility": true, 
      "tags": [] 
     }, 
     { 
      "id": 941926, 
      "name": "Manuel", 
      "description": { 
       "languages": [ 
        "es", 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 0, 
      "basket_amount": 0, 
      "image": { 
       "id": 1969843, 
       "template_id": 1 
      }, 
      "activity": "Property", 
      "sector": "Housing", 
      "use": "to buy two acres of land.", 
      "location": { 
       "country_code": "GT", 
       "country": "Guatemala", 
       "town": "Solola", 
       "geo": { 
        "level": "town", 
        "pairs": "14.666667 -91.25", 
        "type": "point" 
       } 
      }, 
      "partner_id": 246, 
      "posted_date": "2015-09-04T08:10:02Z", 
      "planned_expiration_date": "2015-10-04T08:10:02Z", 
      "loan_amount": 800, 
      "borrower_count": 1, 
      "lender_count": 0, 
      "bonus_credit_eligibility": true, 
      "tags": [ 
       { 
        "name": "volunteer_like" 
       } 
      ] 
     }, 
     { 
      "id": 942158, 
      "name": "Tahira", 
      "description": { 
       "languages": [ 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 150, 
      "basket_amount": 0, 
      "image": { 
       "id": 1969161, 
       "template_id": 1 
      }, 
      "activity": "Sewing", 
      "sector": "Services", 
      "themes": [ 
       "Underfunded Areas" 
      ], 
      "use": "buy a new sewing machine for her clothes sewing center.", 
      "location": { 
       "country_code": "PK", 
       "country": "Pakistan", 
       "town": "Multan", 
       "geo": { 
        "level": "town", 
        "pairs": "30.195556 71.475278", 
        "type": "point" 
       } 
      }, 
      "partner_id": 247, 
      "posted_date": "2015-09-04T07:00:05Z", 
      "planned_expiration_date": "2015-10-04T07:00:05Z", 
      "loan_amount": 400, 
      "borrower_count": 1, 
      "lender_count": 5, 
      "bonus_credit_eligibility": false, 
      "tags": [] 
     }, 
     { 
      "id": 942166, 
      "name": "Sareoun's Group", 
      "description": { 
       "languages": [ 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 0, 
      "basket_amount": 0, 
      "image": { 
       "id": 1970161, 
       "template_id": 1 
      }, 
      "activity": "Home Appliances", 
      "sector": "Personal Use", 
      "themes": [ 
       "Water and Sanitation" 
      ], 
      "use": "to buy a water filter to provide safe drinking water for their family.\t.", 
      "location": { 
       "country_code": "KH", 
       "country": "Cambodia", 
       "town": "Kandal", 
       "geo": { 
        "level": "town", 
        "pairs": "13 105", 
        "type": "point" 
       } 
      }, 
      "partner_id": 311, 
      "posted_date": "2015-09-04T07:00:05Z", 
      "planned_expiration_date": "2015-10-04T07:00:05Z", 
      "loan_amount": 225, 
      "borrower_count": 5, 
      "lender_count": 0, 
      "bonus_credit_eligibility": false, 
      "tags": [] 
     }, 
     { 
      "id": 942156, 
      "name": "Aiza", 
      "description": { 
       "languages": [ 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 75, 
      "basket_amount": 0, 
      "image": { 
       "id": 1970147, 
       "template_id": 1 
      }, 
      "activity": "Farming", 
      "sector": "Agriculture", 
      "use": "to buy fertilizers and other farm supplies", 
      "location": { 
       "country_code": "PH", 
       "country": "Philippines", 
       "town": "Kalibo, Aklan", 
       "geo": { 
        "level": "town", 
        "pairs": "13 122", 
        "type": "point" 
       } 
      }, 
      "partner_id": 145, 
      "posted_date": "2015-09-04T07:00:03Z", 
      "planned_expiration_date": "2015-10-04T07:00:03Z", 
      "loan_amount": 225, 
      "borrower_count": 1, 
      "lender_count": 2, 
      "bonus_credit_eligibility": true, 
      "tags": [] 
     }, 
     { 
      "id": 942160, 
      "name": "Magu Group", 
      "description": { 
       "languages": [ 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 0, 
      "basket_amount": 0, 
      "image": { 
       "id": 1970122, 
       "template_id": 1 
      }, 
      "activity": "Used Clothing", 
      "sector": "Clothing", 
      "use": "to buy fast moving second hand clothes to meet her customer requirement.", 
      "location": { 
       "country_code": "TZ", 
       "country": "Tanzania", 
       "town": "Dar es Salaam", 
       "geo": { 
        "level": "town", 
        "pairs": "-6.8 39.283333", 
        "type": "point" 
       } 
      }, 
      "partner_id": 87, 
      "posted_date": "2015-09-04T07:00:02Z", 
      "planned_expiration_date": "2015-10-04T07:00:02Z", 
      "loan_amount": 525, 
      "borrower_count": 2, 
      "lender_count": 0, 
      "bonus_credit_eligibility": true, 
      "tags": [] 
     }, 
     { 
      "id": 942142, 
      "name": "Mwanamiraji", 
      "description": { 
       "languages": [ 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 0, 
      "basket_amount": 0, 
      "image": { 
       "id": 1970130, 
       "template_id": 1 
      }, 
      "activity": "Food Stall", 
      "sector": "Food", 
      "use": "to purchase a stock of tomatoes, onions, mangoes and green vegetables for resale", 
      "location": { 
       "country_code": "KE", 
       "country": "Kenya", 
       "town": "Tiribe", 
       "geo": { 
        "level": "town", 
        "pairs": "1 38", 
        "type": "point" 
       } 
      }, 
      "partner_id": 164, 
      "posted_date": "2015-09-04T06:30:04Z", 
      "planned_expiration_date": "2015-10-04T06:30:04Z", 
      "loan_amount": 200, 
      "borrower_count": 1, 
      "lender_count": 0, 
      "bonus_credit_eligibility": true, 
      "tags": [] 
     }, 
     { 
      "id": 940065, 
      "name": "Tubogorane Group", 
      "description": { 
       "languages": [ 
        "fr", 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 0, 
      "basket_amount": 0, 
      "image": { 
       "id": 1967147, 
       "template_id": 1 
      }, 
      "activity": "Food", 
      "sector": "Food", 
      "themes": [ 
       "Conflict Zones" 
      ], 
      "use": "to increase his working capital and buy a large quantity of fruit and cheese for sale.", 
      "location": { 
       "country_code": "BI", 
       "country": "Burundi", 
       "town": "Bujumbura", 
       "geo": { 
        "level": "town", 
        "pairs": "-3.5 30", 
        "type": "point" 
       } 
      }, 
      "partner_id": 162, 
      "posted_date": "2015-09-04T06:20:05Z", 
      "planned_expiration_date": "2015-10-04T06:20:05Z", 
      "loan_amount": 3050, 
      "borrower_count": 19, 
      "lender_count": 0, 
      "bonus_credit_eligibility": false, 
      "tags": [] 
     }, 
     { 
      "id": 940076, 
      "name": "Wend Loiim Taore 2 Group", 
      "description": { 
       "languages": [ 
        "fr", 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 50, 
      "basket_amount": 0, 
      "image": { 
       "id": 1967173, 
       "template_id": 1 
      }, 
      "activity": "Clothing Sales", 
      "sector": "Clothing", 
      "use": "to buy pagnes (traditional, wrap-around clothing) and soap for sale.", 
      "location": { 
       "country_code": "BF", 
       "country": "Burkina Faso", 
       "town": "Ouagadougou Boulmiougou", 
       "geo": { 
        "level": "town", 
        "pairs": "13 -2", 
        "type": "point" 
       } 
      }, 
      "partner_id": 398, 
      "posted_date": "2015-09-04T06:20:05Z", 
      "planned_expiration_date": "2015-10-04T06:20:05Z", 
      "loan_amount": 1850, 
      "borrower_count": 5, 
      "lender_count": 2, 
      "bonus_credit_eligibility": false, 
      "tags": [] 
     }, 
     { 
      "id": 942145, 
      "name": "Adham", 
      "description": { 
       "languages": [ 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 25, 
      "basket_amount": 0, 
      "image": { 
       "id": 1970127, 
       "template_id": 1 
      }, 
      "activity": "Barber Shop", 
      "sector": "Services", 
      "use": "to buy new salon equipment.", 
      "location": { 
       "country_code": "PS", 
       "country": "Palestine", 
       "town": "Bethlehem", 
       "geo": { 
        "level": "town", 
        "pairs": "31.92157 35.203285", 
        "type": "point" 
       } 
      }, 
      "partner_id": 289, 
      "posted_date": "2015-09-04T06:10:04Z", 
      "planned_expiration_date": "2015-10-04T06:10:04Z", 
      "loan_amount": 3000, 
      "borrower_count": 1, 
      "lender_count": 2, 
      "bonus_credit_eligibility": false, 
      "tags": [ 
       { 
        "name": "user_favorite" 
       } 
      ] 
     }, 
     { 
      "id": 941929, 
      "name": "Silvia Yesenia", 
      "description": { 
       "languages": [ 
        "es", 
        "en" 
       ] 
      }, 
      "status": "fundraising", 
      "funded_amount": 0, 
      "basket_amount": 0, 
      "image": { 
       "id": 1969848, 
       "template_id": 1 
      }, 
      "activity": "Retail", 
      "sector": "Retail", 
      "themes": [ 
       "Conflict Zones" 
      ], 
      "use": " to buy vegetables, legumes, staple grains and meat, amongst other items, to offer a better service to her customers", 
      "location": { 
       "country_code": "SV", 
       "country": "El Salvador", 
       "geo": { 
        "level": "country", 
        "pairs": "13.833333 -88.916667", 
        "type": "point" 
       } 
      }, 
      "partner_id": 81, 
      "posted_date": "2015-09-04T06:10:03Z", 
      "planned_expiration_date": "2015-10-04T06:10:03Z", 
      "loan_amount": 500, 
      "borrower_count": 1, 
      "lender_count": 0, 
      "bonus_credit_eligibility": true, 
      "tags": [] 
     } 
    ] 
} 

回答

2

loans阵列处于顶层JSON对象,所以:

NSArray *loans = jsonObject[@"loans"]; 
for (NSDictionary *loan in loans) { 
    NSNumber *ident = loan[@"id"]; 
    NSString *name = loan[@"name"]; 
    NSDictionary *description = loan[@"description"]; 
    NSDictionary *location = loan[@"location"]; 
    NSString *country = location[@"country"]; 
    // etc. 
} 
+0

由于它的伟大工程,唯一的问题是我没有得到国家,对。当我使用,NSDictionary *位置=贷款[@“位置”];它给了我国家和country_code。我只想要“国家”。与geo同样的问题(我只想要配对) – user580950

+0

@ user580950对于另一个集合中的每个集合(即字典/数组内的字典/数组),您需要使用类似的模式。我会更新我的答案,展示如何让国家走出去。 – trojanfoe

+0

非常感谢:) – user580950

0

以及你的loans对象是一个字典数组。

  1. 通过[JsonObject objectForKey:@"loans"]你正在获得一个数组。
  2. 那么你需要迭代期望的结果。

    NSArray * arrloans = [JsonObject objectForKey:@“loans”];

    for (NSDictionary *dicloans in arrloans) { 
        NSString *name = [JsonObject objectForKey:@"name"]; 
        NSString *countryCode = [[JsonObject objectForKey:@"location"]objectForKey:@"country_code"]; } 
    
1
NSData *allCourseData=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]]; 
NSError *error; 
NSMutableDictionary *JsonObject = [NSJSONSerialization 
            JSONObjectWithData:allCourseData options:NSJSONReadingMutableContainers 
            error:&error]; 




// Get loans array from JsonObject 
NSArray *loanJsonObject = JsonObject[@"loans"]; 

// Create a new loans array for store the new items 
NSMutableArray *newLoans = [NSMutableArray array]; 

// Loop loanJsonObject to get items and create new ones 
// for then store them in the new loans array 
for (NSDictionary *loan in loanJsonObject) { 
    NSDictionary *newLoan = @{ 
          @"name": loan[@"name"], 
          @"status": loan[@"status"], 
          @"sector": loan[@"sector"], 
          @"languages": loan[@"description"][@"languages"], 
          @"location": @{ 
           @"country": loan[@"location"][@"country"], 
           @"geo": @{ 
            @"pairs": loan[@"location"][@"geo"][@"pairs"] 
           } 
          } 
          }; 

    [newLoans addObject:newLoan]; 
} 

// the new loans 
NSLog(@"newLoans: %@", newLoans); 
+0

我检查它,它显示所有记录我如何nslog一次记录一次?我试过以下但它不工作 NSArray * loans = JsonObject [@“newLoans”]; (NSDictionary *贷款贷款){ NSNumber * languages = loan [@“languages”]; NSString * name = loan [@“name”]; NSDictionary * pairs = loan [@“pairs”]; NSDictionary * sector = loan [@“sector”]; NSString * country = loan [@“country”]; NSString * status = loan [@“status”]; //等 NSLog(@“languages =%@”,languages); } – user580950