从多维JSON数组中检索值

时间:2022-04-25 21:29:50

Hi i am trying to get the subject name value out of the following JSON file (dataArray), which I have already run through NSJSONSERIALISATION:

Hi,我正在尝试从以下JSON文件(dataArray)中获取subject name值,我已经通过NSJSONSERIALISATION运行了:

-0:  {
id: "55edc05848177ec741daf79e"
firstName: "Brad"
rating: 4.2
lessons: 5
text: "Lessons, they're yours take it"
-subjects: [4]
     -0:  {
     name: "Indonesian"
     pricePerHour: "500000"
     }
     -1:  {
     name: "Diving"
     pricePerHour: "700000"
     }

But am not able to access if using the indexPath and dot notation I have used for other elements. I'm understanding it must because I have to access an NSDictionary element within two arrays, however haven't been able to find the correct code to do this (though have looked a lot on here, most examples are for much simpler cases). Here is the code I have been trying, but as I mentioned I now see that this code doesn't cover the array within array of the JSON. How should I modify this to get the value:

但是如果使用我在其他元素中使用的indexPath和点表示法,则无法访问。我之所以理解它,是因为我必须在两个数组中访问NSDictionary元素,但是还没有找到正确的代码来实现这一点(尽管在这里已经看过很多次了,大多数示例都是针对更简单的情况)。这是我一直在尝试的代码,但正如我所提到的,我现在看到,这段代码并没有涵盖JSON数组中的数组。我应该如何修改它来获取值:

// Load and display subjects
    UILabel *subjectLabel = (UILabel *)[cell viewWithTag:103];
    NSString * subject1String = [dataArray[indexPath.row] valueForKeyPath: @"subjects.0.name"];

    subjectLabel.text = [NSString stringWithFormat:@"%@", subject1String];

1 个解决方案

#1


0  

I think this is not valid Json. Print response as you are getting from the server end. And for getting name : you need to add loop.

我认为这不是有效的Json。在服务器端获取响应时打印响应。要获取名称,需要添加循环。

#1


0  

I think this is not valid Json. Print response as you are getting from the server end. And for getting name : you need to add loop.

我认为这不是有效的Json。在服务器端获取响应时打印响应。要获取名称,需要添加循环。