2015-09-04 284 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