0
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [.allowFragments])
        if let responseJSON = responseJSON as? [String:Any] {
            if let tJsonObj = xResponse["d"] as? [[String:Any]] {
               // not working here...
            }
        }

The tJsonObj variable does not get my json array content. My json looks like this:

{"d": "[{\"title\":\"xxx\",\"timestamp\":\"2017-10-16 23:53:40\"},{\"title\":\"Mein Test iPhone 7\",\"timestamp\":\"2017-10-17 18:16:24\"}]"}

I hope someone can help - thanks!

1

2 Answers 2

2

The value for key d is another JSON string. You need to use JSONSerialization twice

do {
  if let responseJSON = try JSONSerialization.jsonObject(with: data) as? [String:Any],
     let tJsonObj = responseJSON["d"] as? String {
        if let innerJSON = try JSONSerialization.jsonObject(with: Data(tJsonObj.utf8)) as? [[String:Any]] { 
           for item in innerJSON {
              print(item)
           }
        }
  }
} catch {
  print(error)
}
Sign up to request clarification or add additional context in comments.

Comments

0

The inner JSON for d looks escaped. Valid JSON should look something like:

{"d": "[{"title":"xxx","timestamp":"2017-10-16 23:53:40"},...

Where is your JSON coming from?

1 Comment

The JSON coming from my server. Objects are created with JavaScriptSerializer. Im sure the JSON ist valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.