0

I have this JSON file:

      var Point :

      [   
         {
         "id": 1,
         "name": "A",
         "LastUpdate": 1468011600,
         "position": [36.8656974, 10.1687314]},

         {
         "id": 1,
         "name": "A",
         "LastUpdate": 1468397003,
         "position": [36.9009882, 10.3009531]
         },
         {
         "id": 1,
         "name": "A",
         "LastUpdate": 1467590490,
         "position": [37.1691357, 10.0349865]
         }
          ]

and I need to read this data in my viewController, I tried to work with SwiftyJSON but I failed. PS: I code with swift and not objective-C Any solutions?

AMAN77
  • 6,218
  • 9
  • 45
  • 60

3 Answers3

1

Why not simply using Alamofire. It's simple framework and you can install it using Cocoa pods. Here is the example of using Alamofire:

Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
         .responseJSON { response in
             print(response.request)  // original URL request
             print(response.response) // URL response
             print(response.data)     // server data
             print(response.result)   // result of response serialization

             if let JSON = response.result.value {
                 print("JSON: \(JSON)")
             }
         }
0

Try this to convert string to NSDictionary

func convertStringToDictionary(text: String) -> [String:AnyObject]? {
  if let data = text.dataUsingEncoding(NSUTF8StringEncoding) {
    do {
        return try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject]
    } catch let error as NSError {
        print(error)
    }
  }
  return nil
}

To remove the syntax errors from your submitted code replace curly braces with square brackets.

var Point = [   
     [
     "id": 1,
     "name": "A",
     "LastUpdate": 1468011600,
     "position": [36.8656974, 10.1687314]],

     [
     "id": 1,
     "name": "A",
     "LastUpdate": 1468397003,
     "position": [36.9009882, 10.3009531]
     ],
     [
     "id": 1,
     "name": "A",
     "LastUpdate": 1467590490,
     "position": [37.1691357, 10.0349865]
     ]
  ]
LIH
  • 933
  • 2
  • 10
  • 25
0

You can use this great library. This library provide parsing from JSON to Dictionary very simple (just 1-2 line of code).

P.S. Also i recommended use this library to mapping Dictionary to Object

UPDATE To install swiftyJSON in podfile

platform :ios, '8.0'
use_frameworks!

pod 'SwiftyJSON'

Then sudo pod install

RrrangeTop
  • 312
  • 1
  • 10