当前位置: 动力学知识库 > 问答 > 编程问答 >

Access multidimensional arrays in objective-c

问题描述:

I have JSON from the Google Speech API. I made this an array by using NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonResponse options: NSJSONReadingMutableContainers error: &e];

Now, my array looks like this (dumped using NSLog(@"%@",jsonArray) ):

{

result = (

{

alternative = (

{

confidence = "0.88664246";

transcript = test;

},

{

transcript = tests;

},

{

transcript = teste;

},

{

transcript = pest;

},

{

transcript = Test;

}

);

final = 1;

}

);

"result_index" = 0;

}

I need to access the first object, or this part:

transcript = "test";

How could I access this?

网友答案:

You would say:

NSLog(@"%@", jsonArray[@"result"][0][@"alternative"][0][@"transcript"]);

Please note that the root object is a dictionary, not an array.

I have a feeling you will need a more generic way of accessing it, so consider creating local objects (perhaps CoreData objects) that represent the objects in the JSON schema, so you can instantiate them and access them normally, instead of by traversing arrays and dictionaries.

分享给朋友:
您可能感兴趣的文章:
随机阅读: