Converting json to html table

Does anyone know an easy way to convert json to an HTML table in Swift 2?

That would be entirely dependant on what your JSON looked like.

Accepted Answer
{
    "columns":[
        {"label":"Group Id","type":"STRING"},
        {"label":"User Id","type":"STRING"},
        {"label":"Device Id","type":"STRING"},
        {"label":"Group Name","type":"STRING"},
        {"label":"User Name","type":"STRING"},
        {"label":"Email Address","type":"STRING"},
        {"label":"Device Name","type":"STRING"},
        {"label":"Device Status","type":"STRING"},
        {"label":"Created When","type":"DATETIME"},
        {"label":"Deleted When","type":"DATETIME"},
        {"label":"Device Type","type":"NUMBER"},
        {"label":"OS Name","type":"STRING"},
        {"label":"Manufacturer","type":"STRING"},
        {"label":"Client Version","type":"STRING"},
        {"label":"Platform Type","type":"STRING"},
        {"label":"Workspace Type","type":"STRING"},
        {"label":"Number Of Sync Assets","type":"NUMBER"},
        {"label":"Number Of Backup Assets","type":"NUMBER"},
        {"label":"First Backup Start Date","type":"DATETIME"},
        {"label":"First Backup End Date","type":"DATETIME"},
        {"label":"Last Contact Time","type":"DATETIME"}
    ],
    "rows":[
        {
            "values":[
                "137260229254447104",
                "137260231938801664",
                "",
                "testgroup1415989388599",
                "with role Admin,user2WViRiQVyU",
                "AdminAYKHpd077D@email.com",
                "null",
                "0",
                "",
                "",
                "",
                "null",
                "null",
                "null",
                "",
                "SYNC",
                "0",
                "0",
                "",
                "",
                ""
            ]
        },
        {
            "values":[
                "137260229254447104",
                "137260239085895680",
                "",
                "testgroup1415989388599",
                "with role Data Admin,useryCEif5a4pI",
                "Data-AdminUQjAIwXF0l@email.com",
                "null",
                "0",
                "",
                "",
                "",
                "null",
                "null",
                "null",
                "",
                "SYNC",
                "0",
                "0",
                "",
                "",
                ""
            ]
        },
        {
            "values":[
                "137260229254447104",
                "137260244995670016",
                "",
                "testgroup1415989388599",
                "with role User,useryP0WDJwBup",
                "UserfC9HDyG5NQ@email.com",
                "null",
                "0",
                "",
                "",
                "",
                "null",
                "null",
                "null",
                "",
                "SYNC",
                "0",
                "0",
                "",
                "",
                ""
            ]
        },
        {
            "values":[
                "137260229254447104",
                "137260244995670016",
                "137260251425540096",
                "testgroup1415989388599",
                "with role User,useryP0WDJwBup",
                "UserfC9HDyG5NQ@email.com",
                "GjYjqLAGPd",
                "1",
                "1415952000000",
                "",
                "",
                "windowds7",
                "Hewlett-Packard",
                "1.01",
                "",
                "BACKUP",
                "0",
                "0",
                "",
                "",
                ""
            ]
        }
    ],
      "countSync":3,
     "countPC":1,
     "countMobile":0,
    "rowCount":4
}


Sample JSON above.


UPDATED: I've managed to do it this way (not sure if this is the best way or not, but it works!)



var jsonResult: NSDictionary
                var htmlTableData: NSString = "<html>\n<head>\n<style>table, th, td {border: 1px solid black;border-collapse:collapse;}\nth, td{padding: 5px;\ntext-align: center;}\ntable#t01 tr:nth-child(even) {background-color: #eee;}\ntable#t01 tr:nth-child(odd) {background-color:#fff;}\ntable#t01 th{background-color: black;color: white;}\n</style>\n</head>\n<body>\n<table id=\"t01\">\n<caption>DEVICE STATUS REPORT</caption>\n<tr>"
             
                do
                 
                {
                    try jsonResult = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
                 if let columnsArray = jsonResult["columns"] as? NSArray
                    {
                        for columns in columnsArray
                        {
                            let labelHeading = columns.objectForKey("label")!
                            htmlTableData = "\(htmlTableData)<th>\(labelHeading)</th>\n"
                        }
                        htmlTableData = "\(htmlTableData)</tr>\n"
                    }
                 
                    if let rowsArray = jsonResult["rows"] as? NSArray
                    {
                        for rows in rowsArray
                        {
                            let rowValues = rows.objectForKey("values")! as? NSArray
                            htmlTableData = "\(htmlTableData)<tr>"
                         
                            for value in rowValues!
                            {
                                htmlTableData = "\(htmlTableData)<td>\(value)</td>\n"
                            }
                            htmlTableData = "\(htmlTableData)</tr>\n"
                        }
                    }
                 
                    htmlTableData = "\(htmlTableData)\n</table>\n</body>\n</html>"
                    print(htmlTableData)
                 
                    dispatch_async(dispatch_get_main_queue(),{
                        SVProgressHUD.dismiss()
                        self.webView_Login.loadHTMLString(htmlTableData as String, baseURL: nil)
                        self.webView_Login.hidden = false
                    })
                }
                catch let error as NSError
                {
                    dispatch_async(dispatch_get_main_queue(),{
                        SVProgressHUD.dismiss()
                        self.showAlert(message: "\(error.localizedDescription)")
                    })
                }
            }

Try to avoid things like NSArray if you're writing new Swift code. Be explicit about your types whenever possible.



print("<table><thead>")
if let columns = json["columns"] as? [[String : String]] {
    for column in columns {
        let label = column["label"]!
        print("<th>\(label)</th>")
    }
}
print("</thead><tbody>")
if let rows = json["rows"] as? [[String : [String]]] {
    for row in rows {
        print("<tr>")
        for value in row["values"]! {
            print("  <td>\(value)</td>", terminator: "")
        }
        print("</tr>")
    }
}
print("</tbody></table>")

Thanks, good point!

Converting json to html table
 
 
Q