-1

I have a page that contains these json data :

{
   "time": "07:08:27 AM",
   "milliseconds_since_epoch": 1425539307783,
   "date": "03-05-2015"
},
{
   "time": "07:08:27 AM",
   "milliseconds_since_epoch": 1425539307783,
   "date": "03-05-2014"
},

and this is the code I use for parsing data :

 // 1
    let urlAsString = "http://192.168.1.35/ios"
    let url = NSURL(string: urlAsString)!
    let urlSession = NSURLSession.sharedSession()

    //2
    let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
        if (error != nil) {
            println(error.localizedDescription)
        }
        var err: NSError?

        // 3
        if var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary {
        if (err != nil) {
            println("JSON Error \(err!.localizedDescription)")
        }

        // 4
        let jsonDate: String! = jsonResult["date"] as NSString
        let jsonTime: String! = jsonResult["time"] as NSString

        dispatch_async(dispatch_get_main_queue(), {
                    println(jsonDate)
        })
        }
    })
    // 5
    jsonQuery.resume()

The problem is that it only works if json data to be like this:

{
   "time": "07:08:27 AM",
   "milliseconds_since_epoch": 1425539307783,
   "date": "03-05-2015"
},

and for multiple json data it stop working.What's wrong ?

Thanks

3
  • check stackoverflow.com/questions/24026305/… Commented Mar 5, 2015 at 7:27
  • You may be missing an array wrapper around your json. Try [{...},{...}] instead of just {...},{...}. Commented Mar 5, 2015 at 7:31
  • @Alan I tried [{...},{...}] but it stop working even with single json data. Commented Mar 5, 2015 at 7:57

1 Answer 1

0

Consider validating your JSON here.

{
   "time": "07:08:27 AM",
   "milliseconds_since_epoch": 1425539307783,
   "date": "03-05-2015"
}

is a valid JSON.

enter image description here

while

{
    "time": "07:08:27 AM",
    "milliseconds_since_epoch": 1425539307783,
    "date": "03-05-2015"
},
{
    "time": "07:08:27 AM",
    "milliseconds_since_epoch": 1425539307783,
    "date": "03-05-2014"
}

is not.

enter image description here

Edit:

JSON arrays are written inside square brackets. Your JSON should be:

[
    {
        "time": "07:08:27 AM",
        "milliseconds_since_epoch": 1425539307783,
        "date": "03-05-2015"
    },
    {
        "time": "07:08:27 AM",
        "milliseconds_since_epoch": 1425539307783,
        "date": "03-05-2014"
    }
]

You can refer here and here for more details.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks,But How should I write the correct one for that?
,Thanks,I tried your answer but The code didn't return anything.
could you help me with that?I tried your answer for json data but nothing returns.What's the problem?
Wish I could, but I have never used Swift. You may want to check this link. Good luck :)

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.