需求,从Json数据中解析data数组里所有以Key“gcz”的数据组成数组
1.从服务器获得Json数据
2.通过SwfityJson第三方框架解析Json数据得到:
decodedJsonDict={
"yxnum" : "16",
"num" : "0",
"data" : [
{
"gcz" : "0.0173",
"sj" : "2017-06-26 09:00:00",
"yqmc" : "",
"yx" : null,
"sjbh" : "右幅1-2X轴倾斜"
},
{
"gcz" : "0.0025",
"sj" : "2017-06-26 09:00:00",
"yqmc" : "",
"yx" : null,
"sjbh" : "左幅2-6X轴倾斜"
},
{
"gcz" : "0.0346",
"sj" : "2017-06-26 09:00:00",
"yqmc" : "",
"yx" : null,
"sjbh" : "左幅2-6Y轴倾斜"
}
]
"newtime" : "2017-06-26 09:00:00",
"State" : "NEWDATA_SUCCESS"
}
3.将data这个JsonArray提取出来let dataJson = decodedJsonDict["data"],得到:
dataJson=[
{
"gcz" : "0.0173",
"sj" : "2017-06-26 09:00:00",
"yqmc" : "",
"yx" : null,
"sjbh" : "右幅1-2X轴倾斜"
},
{
"gcz" : "0.0025",
"sj" : "2017-06-26 09:00:00",
"yqmc" : "",
"yx" : null,
"sjbh" : "左幅2-6X轴倾斜"
},
{
"gcz" : "0.0346",
"sj" : "2017-06-26 09:00:00",
"yqmc" : "",
"yx" : null,
"sjbh" : "左幅2-6Y轴倾斜"
}
]
4.创建数组,注意:数组组成的个数要和JsonArray里的个数一致,否则会报out of range的错。
gczArray = String (repeating: "0", count: dataJson.count)
for index in 0...dataJson.count-1 {
gczArray[index] = dataJson[index]["gcz"].string! as String
print(gczArray)
}
就OK了!其实很简单的嘛!_