converting Array of dictionaries into array of objects - ios

i have a string of this form:
[{"LocationsItems":[{"ItemId":4,"LocationId":3,"Qty":34},{"ItemId":19,"LocationId":3,"Qty":55}]}];
i need to convert this to an array of object, i tried this:
let data = convertedData.data(using: .utf8)!
do {
if let jsonArray = try JSONSerialization.jsonObject(with: data, options : .allowFragments) as? [Dictionary<String,Any>]
{
print(jsonArray)
} else {
print("bad json")
}
} catch let error as NSError {
print(error)
}
}
and returned this , which is an array of dictionaries:
[["LocationsItems": <__NSArrayI 0x600003a1c100>(
{
ItemId = 4;
LocationId = 3;
Qty = 34;
},
{
ItemId = 19;
LocationId = 3;
Qty = 55;
}
)
]]
how can i extract the objects from it?
thanks

You can create a struct of LocationItem.
struct LocationItem {
var itemId: Int?
var locationId: Int?
var qty: Int?
}
var innerDictionary = jsonArray[0] as? [String: Any]
var arrayOfDict = innerDictionary["LocationsItems"] as? [[String: Any]]
var locationsItems = arrayOfDict.map {
LocationItem(itemId: $0["ItemId"], locationId: $0["LocationId"], qty: $0["Qty"])
}
Alternatively, you can use Codable:
struct LocationItem: Codable {
var itemId: Int?
var locationId: Int?
var qty: Int?
enum CodingKeys: String, CodingKey {
case itemId = "ItemId"
case locationId = "LocationId"
case qty = "Qty"
}
}
let decoder = JSONDecoder()
do {
let responseStructure = try decoder.decode([[String:[[String:Any]]]], from: data)
} catch {
// failed
}
let locationItems = responseStructure[0]["LocationsItems"]

Related

Not able to parse json without model

I'm making an api call and getting the response like so..
if let data = NSData(contentsOf: NSURL(string: "http://test.chatongo.in/testdata.json")! as URL) {
do {
if let response = try JSONSerialization.jsonObject(with: data as Data, options: []) as? NSDictionary {
print("THE RESPONSE IS: \(response)")
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
And the response I get like so...
THE RESPONSE IS: {
Message = Success;
Status = 200;
data = {
Records = (
{
Id = 1;
collectedValue = 500;
endDate = "10/06/2018";
mainImageURL = "http://iphonedeveloperguide.com/oneinr/project1.jpg";
shortDescription = "This foundation will bring smile on there faces";
startDate = "05/05/2018";
title = "Smile Crowdfunding";
totalValue = 5000;
},
{
Id = 2;
collectedValue = 750;
endDate = "08/06/2018";
mainImageURL = "http://iphonedeveloperguide.com/oneinr/project10.jpg";
shortDescription = "This foundation will help animals";
startDate = "05/05/2018";
title = "Animal Funding";
totalValue = 20000;
}
);
TotalRecords = 10;
};
}
But how do I parse this json and get the individual elements out of it including the image, that I'm not able to figure out.
You need
import UIKit
class ViewController: UIViewController {
var records = [Record]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
URLSession.shared.dataTask(with: URL(string: "http://test.chatongo.in/testdata.json")!) { (data, response, error) in
guard let data = data else { return }
do {
let res = try JSONDecoder().decode(Root.self, from: data)
self.records = res.data.records
print(res.data.records)
// if it's a collection/table wrap the reload here inside DispatchQueue.main.async
}
catch {
print(error)
}
}.resume()
}
}
// MARK: - Empty
struct Root: Codable {
let status: Int
let message: String
let data: DataClass
enum CodingKeys: String, CodingKey {
case status = "Status"
case message = "Message"
case data
}
}
// MARK: - DataClass
struct DataClass: Codable {
let totalRecords: Int
let records: [Record]
enum CodingKeys: String, CodingKey {
case totalRecords = "TotalRecords"
case records = "Records"
}
}
// MARK: - Record
struct Record: Codable {
let id: Int
let title, shortDescription: String
let collectedValue, totalValue: Int
let startDate, endDate: String
let mainImageURL: String
enum CodingKeys: String, CodingKey {
case id = "Id"
case title, shortDescription, collectedValue, totalValue, startDate, endDate, mainImageURL
}
}
Tip : Don't use NS stuff in swift and avoid using Data(contentsOf: as it blocks the main thread
There are multiple ways, one way to create a modelobject of
struct RecordItem : Codable {
var id : Int?
var collectedValue : Int?
var endDate : String?
var mainImageURL: String?
var shortDescription : String?
var title :String?
var startDate : String?
var totalValue : Int?
}
and
struct Records : Codable {
var items : [RecordItem]?
}
and use this. = let item = data [index]
print (item. collectedValue) and so on.
seconds methods, you already created dict, then extract all keys and array objects using ["Key": "Value"] and set to any variable.

How to make model class for dictionary inside array in swift?

Here is my Json:
[ {
"id": 6854,
"name": "Laundry Iron",
"images": [
{
"id": 6856,
"src": "https://abcd.com/yzx/uploads/1750.jpg",
}
],
} ]
how do we make model class for getting "images":["src": "String" ]?. I want to grab "src" I have tried doing like , but it is not working :
class ProductModel {
var title: String?
var regularPrice: Int?
var salePrice: Int?
var productDescroption: String?
var imageUrl: [ImageUrl]?
init(productJsonL: NSDictionary) {
self.title = productJsonL["name"] as? String
self.regularPrice = productJsonL["regular_price"] as? Int
self.salePrice = productJsonL["sale_price"] as? Int
self.productDescroption = productJsonL["description"] as? String
//The problem is here ........
//self.imageUrl = productJsonL["images"]![0]!["src"] as? String
self.imageUrl = ImageUrl(imageUrlJson: (productJsonL["images"]![0] as? NSDictionary)!)
}
}
class ImageUrl {
var source: String?
init(imageUrlJson: NSDictionary) {
self.source = imageUrlJson["src"] as? String
}
}
please correct me with the structure like I have done above so that i can append everything at once in an array ? Thank you in advance!!
You need Codable
struct Root: Codable {
let id: Int
let name: String
let images: [Image]
}
struct Image: Codable {
let id: Int
let src: String // or let src: URL
}
do {
let res = try JSONDecoder().decode([Root].self, from: data)
print(res)
}
catch {
print(error)
}

iterating through plist file and storing data in array or Dictionary

I am new in swift programing. I'm trying to learn plist file. I have a plist file which has data of Country, State and city. I want to iterate through the data of plist file. But I can't understand how to create an array or dictionary to store the data for country, state and City. Can you help me with how to manipulate the following data of plist file.
{
Country = (
{
CountryName = India;
State = (
{
City = (
Ahmedabad,
Vadodara,
Surat,
Aanand,
Bharuch
);
StateName = Gujrat;
},
{
City = (
Mumbai,
Pune,
Nagpur,
Nasik,
Thane
);
StateName = Maharastra;
},
{
City = (
Kochi,
Kanpur,
Alleppey,
Thrissur,
Thiruvananthapuram
);
StateName = Kerala;
}
);
},
// swift 3 using structure. Manually parsing of data.
struct ListData {
var countries : [Country]?
init(dict:[String:AnyObject]) {
if let countryDict = dict["Country"] as? [[String:AnyObject]] {
self.countries = parseArray(dictArray: countryDict)
}
}
func parseArray(dictArray:[[String:AnyObject]]) -> [Country] {
var array = [Country]()
for dict in dictArray {
let country = Country(dict: dict)
array.append(country)
}
return array
}
}
struct Country {
var countryName : String?
var states : [State]?
init(dict:[String:AnyObject]) {
countryName = dict["CountryName"] as? String
if let stateDict = dict["State"] as? [[String:AnyObject]] {
states = parseArray(dictArray: stateDict)
}
}
func parseArray(dictArray:[[String:AnyObject]]) -> [State] {
var array = [State]()
for dict in dictArray {
let state = State(dict: dict)
array.append(state)
}
return array
}
}
struct State {
var stateName : String?
var cities : [String]?
init(dict:[String:AnyObject]) {
self.stateName = dict["StateName"] as? String
if let cityDict = dict["City"] as? [AnyObject] {
cities = parseArray(dictArray: cityDict)
}
}
func parseArray(dictArray:[AnyObject]) -> [String] {
var array = [String]()
for dict in dictArray {
array.append(dict as! String)
}
return array
}
}
var listData : ListData? = nil
if let path = Bundle.main.path(forResource: "Property List" , ofType: "plist") {
let rootDict = NSDictionary(contentsOfFile: path) as? [String:AnyObject]
listData = ListData(dict: rootDict!)
}
// Using swift 4 and codable protocol.
struct ListData : Codable {
var countries : [Country]?
enum CodingKeys : String, CodingKey {
case countries = "Country"
}
}
struct Country : Codable {
var countryName : String?
var states : [State]?
enum CodingKeys : String, CodingKey {
case countryName = "CountryName"
case states = "State"
}
}
struct State : Codable {
var stateName : String?
var cities : [String]?
enum CodingKeys : String, CodingKey {
case stateName = "StateName"
case cities = "City"
}
}
var listData : ListData? = nil
if let url = Bundle.main.url(forResource: "Property List", withExtension: "plist") {
if let data = try? Data(contentsOf: url) {
let decoder = PropertyListDecoder()
do {
listData = try decoder.decode(ListData.self, from: data)
} catch (let err) {
print(err.localizedDescription)
}
}
}
You can use Object mapper
https://github.com/Hearst-DD/ObjectMapper
where, install the pod and then
create your class as
class State : Mappable {
var stateName: String?
var city: [String]?
required init?(map: Map) {}
func mapping(map: Map) {
stateName <- map["StateName"]
city <- map["City"]
}
}
class Country : Mappable {
var countryName: String?
var state: [State]?
required init?(map: Map) {}
func mapping(map: Map) {
countryName <- map["CountryName"]
state <- map["State"]
}
}
class CountryData: Mappable {
var countries: [Country]?
required init?(map: Map) {}
func mapping(map: Map) {
countries <- map["Country"]
}
}
get your plist content in a varibale like
var dictRoot: NSDictionary?
var countryItems: [String:AnyObject]?
if let path = Bundle.main.path(forResource: "MyCountryList" , ofType: "plist") {
dictRoot = NSDictionary(contentsOfFile: path)
}
if dictRoot != nil
{
// Your dictionary contains an array of dictionary
// Now pull an Array out of it.
countryItems = dictRoot as? [String:AnyObject]
tableView.reloadData()
}
let countryData = CountryData(JSON: countryItems)
now you can use it as you wish

How to handle array in model class?

How can make model class for this json data
{
total: 41,
totalPages: 4,
valueData: [
{
id: "23",
lastLogin: "0-Jul-2011 11:27:36 AM",
name: "varii"
},
{
id: "24",
lastLogin: "0-Jul-2015 11:27:36 AM",
name: "sarii"
},
{
id: "25",
lastLogin: "0-Jul-2018 11:27:36 AM",
name: "narii"
} ]
}
class OnResponse {
var total: Int?
var totalPages: Int?
init(response: [String: Any]) {
self.total = response["total"]
self.totalPages = response["totalPages"]
}
}
It's not working how can make it ready for work
and how to pass values to controller to model and how to get value from model
Follow the below class structure
class Response {
var total: Int?
var totalPages: Int?
var valueData: [LoginData]?
init(response: [String: Any]) {
self.total = response["total"]
self.totalPages = response["totalPages"]
var items:[LoginData] = ()
for (data in response["valueData"]) {
let login = LoginData(name: data["name"], lastLogin: data["lastLogin"])
items.append(login)
}
self.valueData = items
}
}
class LoginData {
var name: String?
var lastLogin: String?
init(name: String, lastLogin: String) {
self.name = name
self.lastLogin = lastLogin
}
}
you should use "reverseMatches" to retrieve the array, not the "data". May be you can use a third library to convert your json data to a model, such as Unbox, https://github.com/JohnSundell/Unbox.
Model for Your response :
struct ModelName {
var total: Int?
var totalPage: Int?
var reverseMatches: [LoginDetails]?
}
struct LoginDetails {
var id: String?
var lastLogin: String?
var name: String?
}
Parse the api response and assign the values on the appropriate fields. I have made, all the variables are optional.
Assign values like below.
var obj = Model()
obj.total = response["total"] as? Int
obj should be var, because you are going to mutate the struct values. because struct is value based, not reference based
class DataModel{
var total : Int?
var totalPages : Int?
var valueData : [UserModel]?
init(JSON: [String:Any?]){
self = parser.doParse(JSON: JSON)
}
}
class UserModel{
var id : String?
var lastLogin : String?
var name : String?
}
class parser : NSObject{
class func doParse(JSON: [String:Any?])->DataModel{
let dataModel = DataModel()
dataModel.total = JSON["total"] as? Int
dataModel.totalPages = JSON["totalPages"] as? Int
var userInfo : [UserModel] = []
let valueData : [String:String?]? = JSON["valueData"] as? [String:String?]
if let valueData = valueData{
for dataDict : [String:String?] in valueData{
let itemModel = UserModel()
itemModel.id = dataDict["id"] as? String
itemModel.lastLogin = dataDict["lastLogin"] as? String
itemModel.name = dataDict["name"] as? String
userInfo.append(itemModel)
}
dataModel.valueData = userInfo
}
return dataModel
}
}
class LoginData: NSObject {
let total: Int = 0
let totalPages: Int = 0
let valueData: Array<ValueData> = Array<ValueData>()
override init(total: Int!, totalPages: Int, valueData: Array<ValueData>!) {
self.total = total
self.totalPages = totalPages
self.valueData = valueData
}
}
struct ValueData {
let id: int?
let lastLogin: String?
let name: String?
init(id: int!, lastLogin: string, name: string!)
self.id = id ?? 0
self.lastLogin = lastLogin ?? ""
self.name = name ?? ""
}
}
you should use struct instead of class for creating model object...
advantages of struct over class refer
Why Choose Struct Over Class?
class/24232845
use two struct for holding your data one is for your single total count
and other is for last login detail
struct lastLogin {
let totalCount: (total: Int, totalPages: Int)
let valueArray: [lastLoginDetail]
}
struct lastLoginDetail {
let valueData: (id: String, lastLogin: String,name: String)
}
extension lastLoginDetail {
init(json: [String : String]) throws {
let id = json["id"]
let lastLogin = json["lastLogin"]
let name = json["name"]
let value = (id,lastLogin,name)
self.valueData = value as! (id: String, lastLogin: String, name: String)
}
}
extension lastLogin {
init(json: [String : Any]) throws {
let total = (json["total"] as! NSNumber).intValue
let totalPages = (json["totalPages"] as! NSNumber).intValue
let totalCounts = (total,totalPages)
var userInfo : [lastLoginDetail] = []
// Extract and validate valueData
let valueData = json["valueData"] as? NSArray
if let valueData = valueData{
for dataDict in valueData{
let dic : [String : String] = dataDict as! [String : String]
let lastLoginDeatails = try! lastLoginDetail(json: dic)
userInfo.append(lastLoginDeatails)
}
}
self.valueArray = userInfo
self.totalCount = totalCounts
}
}
func HowToUseModelClass(){
let jsonDic = NSDictionary()
// jsonDic // your json value
let dataValue = try! lastLogin(json: (jsonDic as! [String : Any])) // parsing the data
print(dataValue.totalCount.total)
print(dataValue.totalCount.totalPages)
print(dataValue.valueArray[0].valueData.id)
}

How to store an array that is within an another array to a global variable that has been passed from JSON in Swift?

I have a JSON which receives an array from an API call
Within that array are 3 other arrays:
userDetails, userStats, communities
An example of this API call is:
["communities": <__NSArrayI 0x6000002540a0>(
{
id = 5;
name = South;
},
{
id = 13;
name = HurraHarry;
},
{
id = 15;
name = EnclliffeT;
}
)
, "userStats": {
totalDraws = 3;
totalLosses = 10;
totalWins = 1;
}, "userDetails": {
id = 31;
"user_email" = "steve#gmail.com";
"user_name" = "Steve Base";
}]
I would like to store the array userStats in a variable that I can pass to another VC.
I have a global variable var userStatsArray = [AnyObject]() in my class
and the following code deals with the JSON:
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
print (json!)
if let arr = json?["communities"] as? [[String:String]] {
self.communitiesArray = arr.flatMap { $0["name"]!}
self.communityIdsArray = arr.flatMap { $0["id"]!}
}
if let dict = json?["userDetails"] as? [String:String] {
self.tempPlayerId = [dict["id"]!]
let characterArray = self.tempPlayerId.flatMap { String.CharacterView($0) }
let newPlayerId = String(characterArray)
self.playerId = newPlayerId
}
if let tempArray = json?["userStats"] as? [String:AnyObject]{
print ("here ", tempArray)
}
The print command successfully prints the userStats array with all its headers (totalWins, totalDraws, totalLosses...) -
How do I store this array into my global variable var userStatsArray = [AnyObject]() so I can pass it to another VC?
Better you create one custom class like this, and declare the array with that custom class type. then you cast your userStats object to your custom class type.
class userStats: NSObject {
var totalDraws: NSNumber?
var totalLosses: NSNumber?
var totalWins: NSNumber?
init(totalDraws: NSNumber?, totalLosses: NSNumber?, totalWins: NSNumber?) {
self.totalDraws = totalDraws
self.totalWins = totalWins
self.totalLosses = totalLosses
}
}
var userStatsArray = [userStats]()
// CHANGE YOUR CODE LIKE THIS
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
print (json!)
if let arr = json?["communities"] as? [[String:String]] {
self.communitiesArray = arr.flatMap { $0["name"]!}
self.communityIdsArray = arr.flatMap { $0["id"]!}
}
if let dict = json?["userDetails"] as? [String:String] {
self.tempPlayerId = [dict["id"]!]
let characterArray = self.tempPlayerId.flatMap { String.CharacterView($0) }
let newPlayerId = String(characterArray)
self.playerId = newPlayerId
}
if let tempArray = json?["userStats"]as? userStats {
userSytatsArray.append(tempArray)
}
Take a look at ObjectMapper! With that powerful framework you can create the mappable models of your data returned by the API and let it perform the whole work for you :)
Declare your model classes like this:
class UserInfo: Mappable {
var communities : [Community]?
var stats: UserStats?
var details: UserDetails?
required init?(map: Map) {
}
func mapping(map: Map) {
communities <- map["communities"]
stats <- map["userStats"]
details <- map["userDetails"]
}
}
class Community: Mappable {
var id: Int!
var name: String!
required init?(map: Map) {
}
func mapping(map: Map) {
id <- map["id"]
name <- map["name"]
}
}
class UserStats: Mappable {
var totalDraws : Int!
var totalLosses : Int!
var totalWins : Int!
required init?(map: Map) {
}
func mapping(map: Map) {
totalDraws <- map["totalDraws"]
totalLosses <- map["totalLosses"]
totalWins <- map["totalWins"]
}
}
class UserDetails: Mappable {
var id : Int!
var email : String!
var username : String!
required init?(map: Map) {
}
func mapping(map: Map) {
id <- map["id"]
email <- map["user_email"]
username <- map["user_name"]
}
}
And later just:
let user = UserInfo(JSONString: JSONString)

Resources