LoginSignup
113
113

More than 5 years have passed since last update.

JSONExportでJSONからモデルのコードを生成する

Last updated at Posted at 2016-04-05

JSONExportとは

JSONExportはJSONからモデルのコードを出力してくれるMac用のアプリケーションです。

スクリーンショット 2016-04-05 11.14.08.png

出力できるもの

  1. Java for Android.
  2. Java for Android - to use with Realm.
  3. Swift Classes.
  4. Swift Classes - To use with SwiftyJSON library.
  5. Swift Classes - To use with Realm.
  6. Swift - CoreData.
  7. Swift Sturcutres.
  8. Objective-C - iOS.
  9. Objective-C - MAC.
  10. Objective-C - CoreData.
  11. Objective-C - To use with Realm.

できること

{
  "id": 123456,
  “text”: “Hello!!“,
  "user": {
    "id": 1234,
    "name": “Motokazu Sekine”
  }
}

たとえば、このJSONから、以下のコードが出力されます。

RootClass.swift
//
//  RootClass.swift
//  Model file Generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation


class RootClass : NSObject, NSCoding{

    var id : Int!
    var text : String!
    var user : User!


    /**
     * Instantiate the instance using the passed dictionary values to set the properties values
     */
    init(fromDictionary dictionary: NSDictionary){
        id = dictionary["id"] as? Int
        text = dictionary["text"] as? String
        if let userData = dictionary["user"] as? NSDictionary{
            user = User(fromDictionary: userData)
        }
    }

    /**
     * Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> NSDictionary
    {
        var dictionary = NSMutableDictionary()
        if id != nil{
            dictionary["id"] = id
        }
        if text != nil{
            dictionary["text"] = text
        }
        if user != nil{
            dictionary["user"] = user.toDictionary()
        }
        return dictionary
    }

    /**
    * NSCoding required initializer.
    * Fills the data from the passed decoder
    */
    @objc required init(coder aDecoder: NSCoder)
    {
         id = aDecoder.decodeObjectForKey("id") as? Int
         text = aDecoder.decodeObjectForKey("text") as? String
         user = aDecoder.decodeObjectForKey("user") as? User

    }

    /**
    * NSCoding required method.
    * Encodes mode properties into the decoder
    */
    @objc func encodeWithCoder(aCoder: NSCoder)
    {
        if id != nil{
            aCoder.encodeObject(id, forKey: "id")
        }
        if text != nil{
            aCoder.encodeObject(text, forKey: "text")
        }
        if user != nil{
            aCoder.encodeObject(user, forKey: "user")
        }

    }

}
User.swift
//
//  User.swift
//  Model file Generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation


class User : NSObject, NSCoding{

    var id : Int!
    var name : String!


    /**
     * Instantiate the instance using the passed dictionary values to set the properties values
     */
    init(fromDictionary dictionary: NSDictionary){
        id = dictionary["id"] as? Int
        name = dictionary["name"] as? String
    }

    /**
     * Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> NSDictionary
    {
        var dictionary = NSMutableDictionary()
        if id != nil{
            dictionary["id"] = id
        }
        if name != nil{
            dictionary["name"] = name
        }
        return dictionary
    }

    /**
    * NSCoding required initializer.
    * Fills the data from the passed decoder
    */
    @objc required init(coder aDecoder: NSCoder)
    {
         id = aDecoder.decodeObjectForKey("id") as? Int
         name = aDecoder.decodeObjectForKey("name") as? String

    }

    /**
    * NSCoding required method.
    * Encodes mode properties into the decoder
    */
    @objc func encodeWithCoder(aCoder: NSCoder)
    {
        if id != nil{
            aCoder.encodeObject(id, forKey: "id")
        }
        if name != nil{
            aCoder.encodeObject(name, forKey: "name")
        }

    }

}

超絶に便利です!!

使い方

JSONExportはバイナリで提供されていませんので、プロジェクトをダウンロードして自分でビルドする必要があります。
ビルドには、XcodeのインストールされたMacが必要です。

https://github.com/Ahmed-Ali/JSONExport からCloneするなりダウンロードするなりして、プロジェクトをゲットしましょう。

JSONExport.xcodeprojをXcodeで開きます。

スクリーンショット 2016-04-05 11.24.41.png

メニューから、[Product]->[Archive]を選択します

スクリーンショット 2016-04-05 11.25.20.png

ビルドが開始されます。
ビルドが終了すると、オーガナイザが開くので[Export]ボタンを押します。

スクリーンショット 2016-04-05 11.28.45.png

「Export as a Mac Application」を選択して、[Next]ボタンを押します。

スクリーンショット 2016-04-05 11.30.14.png

出力先を聞かれるので、とりあえずデスクトップを指定しましょう。

スクリーンショット 2016-04-05 11.32.41.png

スクリーンショット 2016-04-05 11.34.12.png

[Export]ボタンを押すと、JSONExportの入ったフォルダができます。

スクリーンショット 2016-04-05 11.35.01.png

このフォルダの中に入っているJSONExportを使うことになります。

スクリーンショット 2016-04-05 11.35.24.png

あとは、JSONExportを起動して、左ペインにJSONのコードを貼り付けて、右下で出力するコードの種類を指定して、[Save]ボタンを押すだけです!

スクリーンショット 2016-04-05 11.42.43.png

ファイルに出力せずに、そのままコードをコピーしてもOKです。

おしまい

JSONからコードをちまちま書いていくのはミスの元ですし、結構辛い作業です。
JSONExportを使って、楽しましょう!

113
113
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
113
113