在 objective-c 中访问多维数组
Access multidimensional arrays in objective-c
我有 JSON 来自 Google 演讲 API。我使用 NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonResponse options: NSJSONReadingMutableContainers error: &e];
将其设为一个数组
现在,我的数组看起来像这样(使用 NSLog(@"%@",jsonArray)
转储):
{
result = (
{
alternative = (
{
confidence = "0.88664246";
transcript = test;
},
{
transcript = tests;
},
{
transcript = teste;
},
{
transcript = pest;
},
{
transcript = Test;
}
);
final = 1;
}
);
"result_index" = 0;
}
我需要访问第一个对象,或者这部分:
transcript = "test";
我如何访问它?
你会说:
NSLog(@"%@", jsonArray[@"result"][0][@"alternative"][0][@"transcript"]);
请注意根对象是字典,不是数组。
我觉得您需要一种更通用的访问方式,因此请考虑创建代表 JSON 模式中的对象的本地对象(可能是 CoreData 对象),这样您就可以实例化它们并访问他们通常,而不是通过遍历数组和字典。
我有 JSON 来自 Google 演讲 API。我使用 NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonResponse options: NSJSONReadingMutableContainers error: &e];
现在,我的数组看起来像这样(使用 NSLog(@"%@",jsonArray)
转储):
{
result = (
{
alternative = (
{
confidence = "0.88664246";
transcript = test;
},
{
transcript = tests;
},
{
transcript = teste;
},
{
transcript = pest;
},
{
transcript = Test;
}
);
final = 1;
}
);
"result_index" = 0;
}
我需要访问第一个对象,或者这部分:
transcript = "test";
我如何访问它?
你会说:
NSLog(@"%@", jsonArray[@"result"][0][@"alternative"][0][@"transcript"]);
请注意根对象是字典,不是数组。
我觉得您需要一种更通用的访问方式,因此请考虑创建代表 JSON 模式中的对象的本地对象(可能是 CoreData 对象),这样您就可以实例化它们并访问他们通常,而不是通过遍历数组和字典。