Parsing data using NSJSONSerialization.JSONObjectWithData() but cannot access Array groups only strings - ios

I've downloaded the data below which is in Json format.
{"name":"Shropshire Outage","nc_lead":"John Smith","dma":"11/111","username":"vwaghx1","status":"REOPENED","sapOrder":"12341245","ccsText":"123","nbPropAtRisk":12,"logs":[{"dateTime":"2016-04-07 20:02:42","valveStatusChangeDateTime":"2016-04-07 20:02:00","user":"vwaghx1","uid":null,"task":"CUSTPOORSUPPLIES","id_log":1489},{"dateTime":"2016-04-07 20:03:35","valveStatusChangeDateTime":"2016-04-07 20:02:00","user":"vwaghx1","uid":1238768765,"task":"PRESSURELOGGER","id_log":1490},{"dateTime":"2016-04-07 20:04:36","valveStatusChangeDateTime":"2016-04-07 20:02:00","user":"vwaghx1","uid":7692466478,"task":"CUSTSUPPLIESRESTOREDSOME","id_log":1491}],"id_event":601,"region":"Shropshire","trigger":"No Supply Call”,”valveOps":[{"dateTime":"2016-04-07 20:06:12","user":"vwaghx1","uid":7866756788,"x":678666,"y":723325,"description”:”Burst main downstream valve","id_valve_op":523},{"dateTime":"2016-04-07 20:05:31","user":"vwaghx1","uid":5674456470,"x":344534,"y":723433,"description":"Valve to separate both rezones","id_valve_op":522},{"dateTime":"2016-04-14 12:32:00","user":"vwaghx1","uid":1234512345,"x":123123,"y":123123,"description":"test","id_valve_op":541}],"images":[{"name":"After improvement.jpg","dateTimeCreated":"2016-04-08 14:10:30","contentType":"image/jpeg","caption":null,"uuid":null,"fileExtension":"jpeg","id_image":661},...]}
However, when I try to parse it using the code below, I can only access the string values such as "name","nc_lead", "region" etc.
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments) as? Array<Dictionary<String, AnyObject>> {
for item in json {
if let dict = item as? Dictionary<String, AnyObject> {
if let nameStr = dict["name"] as? String {
incidentList.valueStr = nameStr
}
if let codeStr = dict["dma"] as? String {
incidentList.valueStr = codeStr
}
if let region = dict[“region”] as? String {
incidentList.valueStr = region
}
if let ncLead = dict[“nc_lead”] as? String {
incidentList.valueStr = ncLead
}
I need to access the group values like "logs", "images" and "valveOps", which have their own string values in arrays.
How can I change my code so that I can access the strings as I am now and also load the groups into arrays?

You can try like this way, declare 3 array first.
var logArr: [[String: AnyObject]] = [[String: AnyObject]]()
var imageArr: [[String: AnyObject]] = [[String: AnyObject]]()
var valveArr: [[String: AnyObject]] = [[String: AnyObject]]()
Now use this array where you are getting response
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments) as? Array<Dictionary<String, AnyObject>> {
for item in json {
if let dict = item as? Dictionary<String, AnyObject> {
if let nameStr = dict["name"] as? String {
incidentList.valueStr = nameStr
}
if let codeStr = dict["dma"] as? String {
incidentList.valueStr = codeStr
}
if let region = dict["region"] as? String {
incidentList.valueStr = region
}
if let ncLead = dict["nc_lead"] as? String {
incidentList.valueStr = ncLead
}
if let logs = dict["logs"] as? [[String: AnyObject]] {
self.logArr = logs
}
if let valveOps = dict["valveOps"] as? [[String: AnyObject]] {
self.valveArr = valveOps
}
if let images = dict["images"] as? [[String: AnyObject]] {
self.imageArr = images
}

Related

how to parse the data from web services and pass it to table view?

In this the web services i need to save and pass it to a table view how to implement this and the getting data also not possible from json and i need to display it on table view as mentioned below in image can anyone help me how to implement this or any suggestion also would be of great help
and my code is shown below
let url = "http://www.json-generator.com/api/json/get/cwqUAMjKGa?indent=2"
var detailsArray :[[String: AnyObject]] = []
var titleName = [String]()
var productName = [String]()
var children = [String]()
var childrenArray :[[String: AnyObject]] = []
var productsArray :[[String:AnyObject]] = []
var name = [String]()
func downloadJsonWithURL() {
let url = NSURL(string: self.url)
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
self.detailsArray = (jsonObj!.value(forKey: "data") as? [[String: AnyObject]])!
print(self.detailsArray)
for item in self.detailsArray{
if let detailDict = item as? NSDictionary {
if let name = detailDict.value(forKey: "name"){
self.titleName.append(name as! String)
print(self.productName)
}
self.childrenArray = (detailDict.value(forKey: "children") as? [[String : AnyObject]])!
for item in self.childrenArray {
if let detailDict = item as? NSDictionary {
if let name = detailDict.value(forKey: "name"){
self.productName.append(name as! String)
}
self.productsArray = (detailDict.value(forKey: "products") as? [[String : AnyObject]])!
for item in self.productsArray{
if let detailDict = item as? NSDictionary {
if let name = detailDict.value(forKey: "name"){
self.name.append(name as! String)
print(self.name)
}
}
}
}
}
}
}
OperationQueue.main.addOperation({
print(self.productName)
print(self.titleName)
print(self.name)
})
}
}).resume()
}

How to iterate array of dictionaries having nested dictionary?

I have JSON
http://maps.googleapis.com/maps/api/directions/json?&origin=28.594517,77.049112&destination=28.654109,77.34395&travelMode=DRIVING&provideRouteAlternatives=false&sensor=false
I want to access all the values in distance which is in steps.
Here is my code
class Router {
var getValue = Double()
func downloadData(complete: downloadComplete) {
let currentURL = URL(string:getURL)!
Alamofire.request(currentURL).responseJSON { (responseJSON) in
let result = responseJSON
// print(result)
if let dict = result.value as? Dictionary<String,AnyObject>{
if let routes = dict["routes"] as? [Dictionary<String, AnyObject>]{
if let legs = routes[0]["legs"] as? [Dictionary<String, AnyObject>]{
if let steps = legs[0]["steps"] as? [Dictionary<String, AnyObject>]{
for index in steps{
if let distance = steps["distance"] as? Dictionary<String,AnyObject>{
if let value = distance["value"] as? Double{
self.getValue = self.getValue + value
print(self.getValue)
}
}
}
}
}
}
}
}
complete()
}
I am getting error in:
if let distance = steps["distance"] as? Dictionary<String,AnyObject>{
which says:
Cannot subscript a value of type '[Dictionary]' with an index of type string
How can I access this?
You are trying to access the value of an array of dictionaries by key. You should change steps["distance"] to index["distance"].
if let distance = index["distance"] as? Dictionary<String,AnyObject>{
if let value = distance["value"] as? Double{
self.getValue = self.getValue + value
print(self.getValue)
}
}
The error is clear.
steps["distance"] is an array of dictionaries, which can be accessed like steps[0],steps[1].
You need to change following line
if let distance = steps["distance"] as? Dictionary<String,AnyObject>
to
if let distance = index["distance"] as? Dictionary<String,AnyObject>

how to get JSON format from array? - swift

How to get JSON format from array? This is my JSON formatter
{
"productName": "Lemonade",
"transaction": ["[2016-08-01 10:23:42] - Get product in warehouse",
"[2016-08-01 10:53:22] - Sent to customer"]
}
I have an error with this code. I don't know how to get JSON format from transaction array.
if let jsonObject = try JSONSerialization.jsonObject(with: returnData, options: .allowFragments) as? [String: AnyObject]
{
valSucceeded = (jsonObject["succeeded"] as? Bool)!;
valResponseCode = (jsonObject["responseCode"] as? String)!;
valResponseDescription = (jsonObject["responseDescription"] as? String)!;
valSerialNumber = (jsonObject["serialNumber"] as? String)!;
valProductName = (jsonObject["productName"] as? String)!;
if let transactionsArray = jsonObject["transactions"] as? Array<AnyObject> {
for transact in transactionsArray
{
if let transactionStr = transact["transactions"] as? String {
valTransactions = transactionStr
} else {
valTransactions = ""
}
}
}
let entryResult = TraceModel(succeeded: valSucceeded,
responseCode: valResponseCode,
responseDescription: valResponseDescription,
serialNumber: valSerialNumber,
productName: valProductName,
transactions: valTransactions);
traceArray.append(entryResult);
}

parse json data with tableview swift Could not cast value of type '__NSCFString' to 'NSArray'

I have a json like this:
{"page":1,"totalPage":1,"listContent":[{"bizID":3,"bizName":"SHELL KEMANGGISAN","bizImage":"http//:www.goog#gmail.com","address":"JL KEMANGGISAN UTAMA, JAKARTA, 11480, INDONESIA","ratingAvg":1.0,"distance":14003691},{"bizID":4,"bizName":"SHELL DAAN MOGOT","bizImage":"http//:www.goog#gmail.com","address":"JL DAAN MOGOT KM 11, JAKARTA, 11710","ratingAvg":3.0,"distance":14004238}]}
I try to catch the result like this but error:
do {
let jsonArr = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSArray
print("jsonArr:\(jsonArr)")
for json in jsonArr {
if let results = json["listContent"] as? NSArray {
for result in results {
let person = Bizz()
person.bizID = result["bizID"] as! String
person.bizName = result["bizName"] as! String
person.bizImage = result["bizImage"] as! String
person.address = result["address"] as! String
person.ratingAvg = result["ratingAvg"] as! String
self.businessBizz.append(person)
}
}
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
} catch {
print("Failed to get Content List: \(error)")
}
the error is:
Could not cast value of type '__NSCFString' (0x10756f2c8) to 'NSArray' (0x10756fb88).
the error at this line:
let jsonArr = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSArray
how to repair it? and get listcontent value.
Your JSON is [String: AnyObject] means Dictionary not an array..
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: [])
if let results = json["listContent"] as? [[String: AnyObject]] {
for result in results {
let person = Bizz()
person.bizID = result["bizID"] as! Int
person.bizName = result["bizName"] as! String
person.bizImage = result["bizImage"] as! String
person.address = result["address"] as! String
person.ratingAvg = result["ratingAvg"] as! Float
self.businessBizz.append(person)
}
}
You should also convert result["ratingAvg"] and result["bizID"] to Int and Float respectively because they are converted to NSNumber upon being parsed.
You can download the playground from here and test that out yourself.

Json parsing in iOS playground do method is not parsing

I am trying to parse some json data into three different arrays based off the label in the json. I seem to be stuck and don't know why my for loop is never being entered. I am new to iOS and am using this to learn swift. Any help will be appreciated.
Here is the code that I am using:
var myPicture = [String]()
var myPath = [String]()
var mylabel = [String]()
let jsonString = "[{\"picture\" : \"Picture 1 \", \"path\": \"Path 1\" , \"label\" : \"Label 1\"}]"
//Convert jsonString to NSData
let myData = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
do{
let promoJson = try NSJSONSerialization.JSONObjectWithData(myData, options:.AllowFragments)
if let promtions = promoJson[""] as? [[String: AnyObject]] {
for promtions in promtions {
if let picture = promtions["picture"] as? String
{
myPicture.append(picture)
if let path = promtions["path"] as? String
{
myPath.append(path)
if let label = promtions["label"] as? String
{
mylabel.append(label)
}
}
}
}
}
}catch {
print("Error with Json: \(error)")
}
print(myPicture.first)
print(myPath.first)
print(mylabel.first)
The results for the print are all nil. So nothing is being appended to the arrays
The if let promtions = promoJson[""] part won't work and would be useless anyway. This is only promoJson that you have to cast to an array of dictionaries.
You weren't that far from the solution, look at my working version of your code:
do {
let promoJson = try NSJSONSerialization.JSONObjectWithData(myData, options: [])
if let promtions = promoJson as? [[String: AnyObject]] {
for promtion in promtions {
if let picture = promtion["picture"] as? String {
myPicture.append(picture)
}
if let path = promtion["path"] as? String {
myPath.append(path)
}
if let label = promtion["label"] as? String {
mylabel.append(label)
}
}
}
} catch let error as NSError {
print(error.debugDescription)
}
Alternative
Now that the issue is resolved, let me suggest you another way: instead of separate arrays for your data, use one array of objects holding your data.
For example, make a struct like this:
struct Promotion {
let picture: String
let path: String
let label: String
}
And an array for instances of this struct:
var myPromotions = [Promotion]()
Now we can decode the JSON, create objects from it then store them in the array:
do {
let promoJson = try NSJSONSerialization.JSONObjectWithData(myData, options: [])
if let promtions = promoJson as? [[String: AnyObject]] {
for promtion in promtions {
if let picture = promtion["picture"] as? String,
path = promtion["path"] as? String,
label = promtion["label"] as? String {
let promo = Promotion(picture: picture, path: path, label: label)
myPromotions.append(promo)
}
}
}
} catch let error as NSError {
print(error.debugDescription)
}
Now look at the content of the array, very convenient:
for promo in myPromotions {
print(promo.label)
print(promo.path)
print(promo.picture)
}
When you are converting it is already an array.
import Foundation
import UIKit
var myPicture = [String]()
var myPath = [String]()
var mylabel = [String]()
let jsonString = "[{\"picture\" : \"Picture 1 \", \"path\": \"Path 1\" , \"label\" : \"Label 1\"}]"
//Convert jsonString to NSData
let myData = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
do{
let promoJson = try NSJSONSerialization.JSONObjectWithData(myData, options:.AllowFragments) as! NSArray
for promtions in promoJson {
if let picture = promtions["picture"] as? String
{
myPicture.append(picture)
if let path = promtions["path"] as? String
{
myPath.append(path)
if let label = promtions["label"] as? String
{
mylabel.append(label)
}
}
}
}
}catch
{
print("Error with Json: \(error)")
}
print(myPicture.first) // "Optional("Picture 1 ")\n"
print(myPath.first) // "Optional("Path 1")\n"
print(mylabel.first) // "Optional("Label 1")\n"
This does the job.

Resources