新手一枚,但是实在是找不到相关的例子来学习了,所以迫不得已提问。
现在的需求是吧类似这样的Json存储在一个字典中,主要是相同ID的需要存在一起。
{
"hy_id": 5,
"company": [
{
"name": "JOVIOLD"
},
{
"name": "DAYCORE"
},
{
"name": "ENDIPINE"
}
]
}
我尝试使用[String:AnyObject]类型的字典,但是改如何判断是相同ID呢?ID和name都可以正常获取到了,但是不知道应该怎么存储的语句
let path: String = NSBundle.mainBundle().pathForResource("jsonData", ofType: "json")!
let nsurl = NSURL(fileURLWithPath: path)
let data:NSData = NSData(contentsOfURL: nsurl)!
let dict = JSON(data:data)
//ToDo
for i in 0...dict.count{
let number = dict[i]["hy_id"].intValue
let numberS = String(number)
let name:String = dict[i]["name"].stringValue
//ToDo
print("\(number)\(name)")
}
我的目的是将这个json包储存到一个字典里,每个hy_id对应一个key,每个name放在对应的value。我现在的代码有问题,可以请您重新写一下如何存储。
Starting from iOS5, JSON can be processed using the
JSONSerialization
class in the Foundation framework, which is specialized in converting JSON to dictionary or dictionary to JSON.I don’t know if you are using SwiftyJSON. If so,
let dict = JSON(data:data)
This JSON construction method is all systematiclet dict = JSON(data:data)
这句JSON的构造方法用的都是系统的JSONSerialization
:If you want to change it to a dictionary, it is very simple to use SwiftJSON, just get the value directly: