需求,從Json數(shù)據(jù)中解析data數(shù)組里所有以Key“gcz”的數(shù)據(jù)組成數(shù)組
1.從服務(wù)器獲得Json數(shù)據(jù)
2.通過SwfityJson第三方框架解析Json數(shù)據(jù)得到:
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這個(gè)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.創(chuàng)建數(shù)組,注意:數(shù)組組成的個(gè)數(shù)要和JsonArray里的個(gè)數(shù)一致劈狐,否則會(huì)報(bào)out of range的錯(cuò)勤哗。
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了彼念!其實(shí)很簡單的嘛屡律!_