I am trying to convert stored coredata
values to JSON
format and the JSON format value need to assign a single variable, because this generated JSON I need to send to server. Below code I tried to get coredata stored values but don’t know how to generate JSON required format.
Getting values from coredata
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
do {
let results = try context.fetch(fetchRequest)
let dateCreated = results as! [Userscore]
for _datecreated in dateCreated {
print("(_datecreated.id!)-(_datecreated.name!)") // Output: 79-b
80-c
78-a
}
} catch let err as NSError {
print(err.debugDescription)
}
Need to Convert Coredata Value to Below JSON format
{
????"status":?true,
????"data":?[
????????{
????????????"id":?"20",
????????????"name":?"a"
????????},
????????{
????????????"id":?"21",
????????????"name":?"b"
????????},
????????{
????????????"id":?"22",
????????????"name":?"c"
????????}
????]
}
See Question&Answers more detail:os