I need to parse a JSON String like this
{
"someData": [{
"title": "Test!",
"content": "hello!"
}, {
"title": "Test 2!",
"content": "hello!"
}],
"otherData": [{
"title": "Hey",
"content": "yes or no"
}]
}
I have some code to parse json. But this code just parse json which is only in one array. I have played with the code, so I can parse an array which is in an array which has text in it (I mean this json on the top).
Now I give up... What I have to do that this can work?
let jsonString = NSString(data: getJSON("URL"), encoding: NSUTF8StringEncoding)
let jsonData = jsonString!.dataUsingEncoding(NSUTF8StringEncoding)!
do {
if let parsed = (try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions(rawValue: 0)) as? [[String : AnyObject]]) {
for items in parsed {
if let item = items as? [String : AnyObject] {
let title = item["title"] as? String
doSomething(title!)
}
}
}
} catch let error as NSError {
print("\(error)")
}
func parseJSON() {
let path = NSBundle.mainBundle().pathForResource("data1", ofType: "json")
let jsonData = NSData(contentsOfFile: path!)! as NSData
do {
let newJson = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers)
if let otherData = newJson["otherData"] as? NSArray {
let newData1 = otherData[0]
if let content = newData1["content"] as? String {
if let title = newData1["title"] as? String {
print(title)
print(content)
}
}
}
if let someData = newJson["someData"] as? NSArray {
var x = 0
while x < someData.count {
if let title = someData[x]["title"] as? String {
if let content = someData[x]["content"] as? String {
print(title)
print(content)
}
}
x++
}
}
}
catch {
}
}
then, in your viewDidLoad:
override func viewDidLoad() {
parseJSON()
}
Edit:
This is assuming that you have the json file named "data1.json" and it is in your project directory like so:
If the json file does not have this name or is in another place in your project directory, it won't work.
Reading a JSON string is quite easy.
There are only two collection types:
{}
[]
All keys in JSON are of type String
by definition therefore the least specific type of an dictionary is [String:AnyObject]
.
In your example the root object is a dictionary {}
containing two keys someData
and otherData
so the line to create a JSON object is supposed to be (no if - let
)
let parsed = try NSJSONSerialization.JSONObjectWithData(jsonData, options:[]) as! [String:AnyObject]
someData
contains an array of dictionaries [{...},{...}]
, in your example all values are strings so you can constrain the object to [[String:String]]
and parse the items with
if let someData = parsed["someData"] as? [[String:String]] {
for anItem in someData {
print(anItem["title"]!)
print(anItem["content"]!)
}
}
For simple structures a third party library like SwiftyJSON
is actually not needed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.