Reputation: 1148
I'm trying to build some JSON in Swift. I've seen some examples of turning a Dictionary into JSON or a class into JSON, but I want to be able to turn data into JSON on the fly. I also have multiple objects I want to store, not just one (which most examples I find seem to do).
Here is my code that creates dictionaries of the objects I want to store:
for topic in topics{
var relEntries = getRelevantEntries(topic.id)
var entryItem:[String:Any] = ["":""]
for entry in relEntries{
var eId = ["id":entry.id]
var eTitle = ["title":entry.title]
entryItem = [
"id":entry.id,
"title":entry.title
]
}
var topicItem:[String:Any] = [
"id":topic.id,
"topicName":topic.name,
"entries":entryItem
]
}
I want to create a JSON Array, where each object includes an int, string, and another JSON Array. I wrote out a demo table to clarify.
[
{ id: "id",
topicName: "topicName",
entries:[
{ id: "id", title: "title" },
{ id: "id2", title: "title2"}
]},
{ id: "id2",
topicName: "topicName2",
entries:[
{ id: "id", title: "title" },
{ id: "id2", title: "title2"}
]}
]
So where do I go from here to combine all my topics into one JSON Array? Thanks.
Upvotes: 0
Views: 1576
Reputation: 536018
You say:
I want to create a JSON Array, where each object includes an int, string, and another JSON Array. I wrote out a demo table to clarify.
[
{ id: "id",
topicName: "topicName",
entries:[
{ id: "id", title: "title" },
{ id: "id2", title: "title2"}
]},
{ id: "id2",
topicName: "topicName2",
entries:[
{ id: "id", title: "title" },
{ id: "id2", title: "title2"}
]}
]
Well, what is that? It's an array ([...]
) of dictionaries ({...}
), where each dictionary has an id
key (with a string value), a topicName
key (with a string value), and an entries
key, where each entries
value is an array containing two dictionaries with an id
key and a title
key (each with a string value).
So your job is merely to construct that structure and serialize it into JSON. That's trivial. Here, as a mockup, I'll demonstrate using literals:
var d1 : [NSObject:AnyObject] = ["id":"id", "topicName":"topicName"]
var d2 : [NSObject:AnyObject] = ["id":"id2", "topicName":"topicName2"]
let dd1 = ["id":"id", "title":"title"]
let dd2 = ["id":"id2", "title":"title2"]
let arr1 = [dd1,dd2]
let arr2 = [dd1,dd2]
d1["entries"] = arr1
d2["entries"] = arr2
let arr = [d1,d2]
let d = try! NSJSONSerialization.dataWithJSONObject(arr, options: [])
let s = NSString(data: d, encoding: NSUTF8StringEncoding)
print(s!)
Output:
[
{"id":"id",
"entries":[
{"id":"id","title":"title"},
{"id":"id2","title":"title2"}
],
"topicName":"topicName"
},
{"id":"id2",
"entries":[
{"id":"id","title":"title"},
{"id":"id2","title":"title2"}
],
"topicName":"topicName2"
}
]
I believe you'll find that that's exactly the equivalent of what you asked for. Now all you have to do is write code that converts your original data structure into the data structure that I constructed above out of literals.
Upvotes: 0
Reputation: 4596
You can use serialization techniques on your custom objects to build Foundation types that represent them.
After that, you can use the NSJSONSerialization
class to generate the json.
Since you have to differentiate between your different classes, there is less except handling with mirrors that might generalize that work for you at this point in time, afaik.
Upvotes: 0