Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在解析JSON时,如何仅使用顶级元素生成数组?_Ios_Json_Swift - Fatal编程技术网

Ios 在解析JSON时,如何仅使用顶级元素生成数组?

Ios 在解析JSON时,如何仅使用顶级元素生成数组?,ios,json,swift,Ios,Json,Swift,使用Alamofire和(尝试使用)SwiftyJSON 假设我有一个JSON,在“顶层”我有两个条目:TopEntry 1和TopEntry 2。然后在每个条目中,我都有一个“标题”和一个“URL” 所以它看起来像这样: { TopEntry 1: { title: someTitle1 URL: someURL1 } TopEntry 2: { title: someTitle2 URL: someURL2 }

使用Alamofire和(尝试使用)SwiftyJSON

假设我有一个JSON,在“顶层”我有两个条目:TopEntry 1和TopEntry 2。然后在每个条目中,我都有一个“标题”和一个“URL”

所以它看起来像这样:

{
TopEntry 1: {
      title: someTitle1
      URL: someURL1
        }
TopEntry 2: {
      title: someTitle2
      URL: someURL2
        }
}
如果我只想得到TopEntry 1和TopEntry 2的值,我如何循环这个数组并为每个索引(I=0、I=1等)分配TopEntry 1、TopEntry 2等。。。到某个NSMutableArray

我编写了以下代码,但似乎没有任何用处:

let nodeIDArray : [String]
var nodeCounter : Int = 0 // My index
for nodeCounter in 0..<9 { // I know there are 10 elements, otherwise I would replace 9 with jsonValue.count , jsonValue being my JSON result
if let jsonValue = result.value {
nodeIDArray[nodeCounter] = jsonValue[nodeCounter].string
}
let nodeIDArray:[字符串]
var nodeCounter:Int=0//我的索引

首先,对于0..中的nodeCounter,我认为您不希望在循环中为jsonValue赋值

尝试通过直接在代码中构建一些示例来了解json结构。然后尝试访问结果。根据实际数据的复杂性构建样本