JSON Objective-c - 对象和数组组合

JSON Objective-c - Objects and Arrays Combination

我的JSONurl是http://api.kivaws.org/v1/loans/search.json?status=fundraising

我的 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 数组以显示以下内容 例如,它将显示名称、显示所有语言、状态,因此输出看起来像
============================
姓名 : "Maria Luisa"
语言:es,en
状态:"fundraising"
扇区:"Retail"
位置

country_code : "NI"


姓名:"Florence" 语言:en
状态:"fundraising"
扇区:"Retail"
位置
country_code : "KE"

我的JSON 供稿

{
    "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": []
        }
    ]
}

好吧,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.
}

你的 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"];  }
    
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);