13.12.2017: PLEASE jump to "Update 3" as I have changed my function !!!

---

from an HTTP Get to my Web API I am getting a JSON response and I need to display the data row by row in a `TableViewController`. But the output is empty, as I think that I am not properly able to get `stdata` out of data\_list. I assume I am doing something wrong with types?

This is the relevant part of the code:

```
func extract_json(_ data: Data){

    // added for UPDATE 2
    let string = String.init(data: data, encoding: .utf8)
    print(string)

    do {
        let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments])
        let data_list = json as? [AnyObject]

        for stdata in data_list! {
            if let StMsgID = stdata["MsgID"] as? String {  // for UPDATE 1: set breakpoint to this line
                if let StSerial = stdata["Serial"] as? String{
                    TableData.append(StMsgID + " : " + StSerial)
                }
            }
        }
    } catch let error as NSError {
        print(error)
        return
    }
    DispatchQueue.main.async(execute: {self.do_table_refresh()})
}

func do_table_refresh()
{
    self.tableView.reloadData()
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return TableData.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    cell.textLabel?.text = TableData[indexPath.row]

    return cell
}

```

This is `JSON` when I display it in the debugger:

```
▿ 1 element
  - 0 : [{"CreatedAt":"01-11-17 10:46","MsgID":"ST88”,”Serial”:”88”},{“CreatedAt":"03-12-17 21:35","MsgID”:”ST99”,”Serial”:”99”}]

```

And this is **data\_list**:

```
▿ Optional<Array<AnyObject>>
  ▿ some : 1 element
    - 0 : [{"CreatedAt":"01-11-17 10:46","MsgID":"ST88”,”Serial”:”88”},{“CreatedAt":"03-12-17 21:35","MsgID”:”ST99”,”Serial”:”99”}]

```

I would expect to see two rows in the TableViewController:

```
ST88 : 88
ST99 : 99

```

What I am getting is an empty table.

Thank you in advance!!!

UPDATE 1:

When I set the breakpoint to line:

```
if let StMsgID = stdata["MsgID"] as? String...

```

I can see the below value, when I drill down into "stdata" -> 0x... -> NSMutableString -> NSString -> NSObject -> isa:

```
[{"CreatedAt":"01-11-17 10:46","MsgID":"ST88”,”Serial”:”88”},{“CreatedAt":"03-12-17 21:35","MsgID”:”ST99”,”Serial”:”99”}]

```

Is it parsed correctly as I still do see both data sets? At this point I would expect to see only the first data set with ST88.

UPDATE 2:

Added the below code to the top of my function:

```
    let string = String.init(data: data, encoding: .utf8)
    print(string)

```

The output is:

```
Optional("[\"[{\\\"CreatedAt\\\":\\\"01-11-17 10:46\\\",\\\"MsgID\\\":\\\"ST88\\\”,\\\”Serial\\\”:\\\”88\\\”},{\\\"CreatedAt\\\":\\\"03-12-17 21:35\\\",\\\"MsgID\\\”:\\\”ST99\\\”,\\\”Serial\\\”:\\\”99\\\”}]\"]")

```

==================================

UPDATE 3:

I have changed my code to use SwiftyJson but still cannot get it working.

This is now the latest format I am using, below the response from my Web API:

```
["{\"inbox\":[{\"CreatedAt\":\"01-11-17 10:46\",\"MsgID\":\"ST88\",\"Serial\":\"88\"},{\"CreatedAt\":\"03-12-17 21:35\",\"MsgID\":\"ST99\",\"Serial\":\"99\"}]}"]

```

As per jsonlint.com this is valid.

This is my new function with SwiftyJson in Swift 3:

```
// using SwiftyJSON !!!
func extract_json(_ data: Data)
{
    do
    {
        let json = try JSON(data: data)

        for obj in json["inbox"]
        {
            let value = obj["MsgID"].string // error here (not compiling)
            print (value)
            TableData.append(value)
        }
    }
    catch let error as NSError
    {
        print(error)
        return
    }

    DispatchQueue.main.async(execute: {self.do_table_refresh()})
}

```

I would expect to see:

```
ST88
ST99

```

It´s not even compiling, the error I am getting for line

```
let value = obj["MsgID"].string

```

is:

```
Value of tuple type '(String, JSON)' has no member 'subscript'

```