导入JSON时对象和数组之间的差异

时间:2015-10-17 21:38:02

标签: ios objective-c json object nsmutablearray

我正在尝试按照this教程从JSON导入数据,并意识到我没有从根本上理解数组和对象之间的区别。将JSON提要捕获到数组后,您通过创建对象做了什么?对象和数组有什么区别?

这是将数组转换为对象的代码。希望有人解释为什么有必要以及你在这里做了什么......

// Parse the JSON that came in
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];
/*If you log out this array, you see something that looks like JSON as in  {
   name = "Googleplex";
   location = "100 Ampitheatre";
   }, etc.
*/
// Loop through Json objects, create objects and add the to our array
for (int i = 0; i < jsonArray.count; i++)
{
    NSDictionary *jsonElement = jsonArray[i];

    // Create a new location object and set its props to JsonElement properties
    Location *newLocation = [[Location alloc] init];
    newLocation.name = jsonElement[@"Name"];
    newLocation.address = jsonElement[@"Address"];

    // Add this question to the locations array
    [_locations addObject:newLocation];
}

0 个答案:

没有答案