Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example handling JSON with SwiftyJSON

I would like to handle json with SwiftJSON, but I stacked. Does anyone show me example code?

I tried to use this library. https://github.com/SwiftyJSON/SwiftyJSON

Although I placed SwiftyJSON.swift in the same project, I have error "No such module "SwiftyJSON"" So correct my code or show me example code handling json from web with swiftyJSON lib.

Here is my code:

import UIKit
import SwiftyJSON // No such module "SwiftyJSON"

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let url = NSURL(string: "http://express.heartrails.com/api/json?method=getPrefectures")

        var request = NSURLRequest(URL: url!)
        var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)

        var json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSDictionary

        var hoge = JSON(data)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Here is my Xcode capture

Screenshot

like image 927
tatsuya.yokoyama Avatar asked Nov 05 '14 10:11

tatsuya.yokoyama


4 Answers

If you added SwiftyJSON.swift to your project, you don't need to import it. It's already available.

Try:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let url = NSURL(string: "http://express.heartrails.com/api/json?method=getPrefectures")
        var request = NSURLRequest(URL: url!)
        var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
        if data != nil {
            var hoge = JSON(data: data!)
            println(hoge)
        }
    }
}
like image 62
rintaro Avatar answered Oct 28 '22 21:10

rintaro


Use this https://github.com/SwiftyJSON/SwiftyJSON version to get up to date SwiftyJSON

If you want to use import SwiftyJSON then you need to add using pod to do this, follow steps

  1. Open terminal and run sudo gem install cocoapods to install cocoapods
  2. From your terminal, go to your project home
  3. Run pod init to initialize Podfile
  4. Open Podfile and paste following command

platform :ios, '8.0' use_frameworks! target 'MyApp' do pod 'SwiftyJSON', '~> 2.2.1' end

  1. Finally run pod install and it will add SwiftyJSON into you project
  2. Close xcode and open .xcworkspace instead of .xcodeproj

Now you are good to go

For more info, SwiftyJSON in cocoapods

like image 32
Omar Faruqe Avatar answered Oct 28 '22 22:10

Omar Faruqe


The issue I've had is not following this bit of the CocoaPods instructions:

Make sure to always open the Xcode workspace instead of the project file when building your project

I was opening the project instead of the workspace which resulted in the No Such Module error.

This went away after opening the workspace.

like image 2
Marcus Leon Avatar answered Oct 28 '22 22:10

Marcus Leon


Hi this is the link to a new Tutorial that explain very well how to working with JSON in Swift.

Parsing JSON the SwiftyJSON Way

like image 1
Alessio Marzoli Avatar answered Oct 28 '22 21:10

Alessio Marzoli