-1
我想解析一个字符串,我手动在swift中使用循环创建。我把它作为一个字符串,然后我厌倦了使用Swifty JSON将该字符串转换为json。当我尝试通过json循环时,我的代码永远不会进入循环。我怀疑有数据类型问题,但我被卡住了。任何帮助,将不胜感激。这里是我作为字符串使用Swift和SwiftyJSON解析JSON字符串
[{
"category_name": "AIR SYSTEM",
"cntpassed": 0,
"cntfailed": 0,
"isfailed": 0,
"isstarted": 1,
"iscomplete": 1,
"isnotcomplete": 0,
"cnttotal": 5
}, {
"category_name": "ENGINE COMPARTMENT",
"cntpassed": 0,
"cntfailed": 0,
"isfailed": 0,
"isstarted": 1,
"iscomplete": 1,
"isnotcomplete": 0,
"cnttotal": 27
}, {
"category_name": "EXTER.",
"cntpassed": 0,
"cntfailed": 0,
"isfailed": 0,
"isstarted": 1,
"iscomplete": 1,
"isnotcomplete": 0,
"cnttotal": 3
}]
我使用SWIFTY JSON方法
let json = JSON(jsonStringAbove)
然后我在迅速
public func jsonFormSectionsArray(jsonString: String) -> Array<String>
{
print("In jsonFormsSectionArray")
var anArray: [String] = []
let json = JSON(jsonString)
print("\nHeres the JSON \(json)")
for (key, subJson) in json {
// My code never gets to this point
if let category = subJson["category_name"].string {
print(category)
anArray.append(category)
}
}
print("PETE --> In Function Array \(anArray)")
return anArray
}
你必须从字符串而不是Json对象创建一个Json数组 – Aditya
@Aditya yes!谢谢,我明白了 – plgelso