[iOS][Android][mac] JSONExportを使ってJSONからモデルクラスを自動生成する

[iOS][Android][mac] JSONExportを使ってJSONからモデルクラスを自動生成する

Clock Icon2017.07.07

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

JSONからモデルクラスを作りたい

サーバーサイドエンジニア:「APIのインターフェース、JSONの内容はこれで良いですか?」
フロントエンジニア:「はい!問題ないです。これで実装を進めます。(さて、〜APIのレスポンスに対応したモデルクラスを作るか...)」

上記のようなやりとりは特にスマホアプリ開発の現場ではよくあることかと思います。 また、昨今のスマホアプリが使用するWeb APIはJSONでやりとりするもの(Content-Type: application/json)が多いです。
通常、フロントエンジニアはサーバーサイドエンジニアとAPI仕様について協議・定義した後、その内容をコードに落とし込むことになります。
その際に地味に面倒なのがJSONの内容に対応したモデルクラスを作る作業です。

「JSONの内容は決まっているのだからこれを使ってモデルを自動生成できないかな...」そんな願いを叶えてくれるツール「JSONExport」をご紹介します。

JSONExportとは

JSONオブジェクトを対応しているプログラミング言語のモデルクラスに変換してくれるMacのデスクトップアプリケーションです。カスタムMITライセンスの下で公開されています。記事執筆時点の最新バージョンは1.0.8です。

対応している言語

記事執筆時点で以下の言語に対応しています。 RealmやSwiftyJSON、ObjectMapperなど人気のライブラリに対応しているのも特徴です。

  1. Java for Android.
  2. Java for Realm Android.
  3. GSON for Android
  4. Swift Classes.
  5. Swift Classes for SwiftyJSON library.
  6. Swift Classes for Realm.
  7. Swift - CoreData.
  8. Swift Structures.
  9. Swift Structures for Gloss
  10. Swift Mappable Classes for (Swift 3) ObjectMapper
  11. Swift Structures for Unbox
  12. Objective-C - iOS.
  13. Objective-C - MAC.
  14. Objective-C - CoreData.
  15. Objective-C for Realm iOS.

起動方法

リポジトリからgit cloneしてXcode 8でビルドするだけです。

画面構成

JSONExportの画面は以下のようになっています。

jsonexport001

使ってみる

使ったJSON

郵便番号検索APIを使って得られた以下のJSONを使わせていただきました。

{
    "message": null,
    "results": [
        {
            "address1": "東京都",
            "address2": "千代田区",
            "address3": "岩本町",
            "kana1": "トウキョウト",
            "kana2": "チヨダク",
            "kana3": "イワモトチョウ",
            "prefcode": "13",
            "zipcode": "1010032"
        }
    ],
    "status": 200
}

上記のJSONを以下の条件で言語毎のモデルクラスに変換してみます。

  • コンストラクターの生成ON
  • ユーティリティーメソッドの生成ON
  • Rootクラス名は「APIResponse」
  • Class Prefiexなし

以下、変換結果です。

Java for Android

//
//  APIResponse.java
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import org.json.*;
import java.util.*;


public class APIResponse{

    private Object message;
    private Result[] results;
    private int status;

    public void setMessage(Object message){
        this.message = message;
    }
    public Object getMessage(){
        return this.message;
    }
    public void setResults(Result[] results){
        this.results = results;
    }
    public Result[] getResults(){
        return this.results;
    }
    public void setStatus(int status){
        this.status = status;
    }
    public int getStatus(){
        return this.status;
    }

    /**
     * Instantiate the instance using the passed jsonObject to set the properties values
     */
    public APIResponse(JSONObject jsonObject){
        if(jsonObject == null){
            return;
        }
        message = jsonObject.opt("message");
        JSONArray resultsJsonArray = jsonObject.optJSONArray("results");
        if(resultsJsonArray != null){
            ArrayList<Result> resultsArrayList = new ArrayList<>();
            for (int i = 0; i < resultsJsonArray.length(); i++) {
                JSONObject resultsObject = resultsJsonArray.optJSONObject(i);
                resultsArrayList.add(new Result(resultsObject));
            }
            results = (Result[]) resultsArrayList.toArray();
        }       status = jsonObject.optInt("status");
    }

    /**
     * Returns all the available property values in the form of JSONObject instance where the key is the approperiate json key and the value is the value of the corresponding field
     */
    public JSONObject toJsonObject()
    {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("message", message);
            if(results != null && results.length > 0){
                JSONArray resultsJsonArray = new JSONArray();
                for(Result resultsElement : results){
                    resultsJsonArray.put(resultsElement.toJsonObject());
                }
                jsonObject.put("results", resultsJsonArray);
            }
            jsonObject.put("status", status);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jsonObject;
    }

}

//
//  Result.java
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import org.json.*;
import java.util.*;


public class Result{

    private String address1;
    private String address2;
    private String address3;
    private String kana1;
    private String kana2;
    private String kana3;
    private String prefcode;
    private String zipcode;

    public void setAddress1(String address1){
        this.address1 = address1;
    }
    public String getAddress1(){
        return this.address1;
    }
    public void setAddress2(String address2){
        this.address2 = address2;
    }
    public String getAddress2(){
        return this.address2;
    }
    public void setAddress3(String address3){
        this.address3 = address3;
    }
    public String getAddress3(){
        return this.address3;
    }
    public void setKana1(String kana1){
        this.kana1 = kana1;
    }
    public String getKana1(){
        return this.kana1;
    }
    public void setKana2(String kana2){
        this.kana2 = kana2;
    }
    public String getKana2(){
        return this.kana2;
    }
    public void setKana3(String kana3){
        this.kana3 = kana3;
    }
    public String getKana3(){
        return this.kana3;
    }
    public void setPrefcode(String prefcode){
        this.prefcode = prefcode;
    }
    public String getPrefcode(){
        return this.prefcode;
    }
    public void setZipcode(String zipcode){
        this.zipcode = zipcode;
    }
    public String getZipcode(){
        return this.zipcode;
    }

    /**
     * Instantiate the instance using the passed jsonObject to set the properties values
     */
    public Result(JSONObject jsonObject){
        if(jsonObject == null){
            return;
        }
        address1 = jsonObject.opt("address1");
        address2 = jsonObject.opt("address2");
        address3 = jsonObject.opt("address3");
        kana1 = jsonObject.opt("kana1");
        kana2 = jsonObject.opt("kana2");
        kana3 = jsonObject.opt("kana3");
        prefcode = jsonObject.opt("prefcode");
        zipcode = jsonObject.opt("zipcode");
    }

    /**
     * Returns all the available property values in the form of JSONObject instance where the key is the approperiate json key and the value is the value of the corresponding field
     */
    public JSONObject toJsonObject()
    {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("address1", address1);
            jsonObject.put("address2", address2);
            jsonObject.put("address3", address3);
            jsonObject.put("kana1", kana1);
            jsonObject.put("kana2", kana2);
            jsonObject.put("kana3", kana3);
            jsonObject.put("prefcode", prefcode);
            jsonObject.put("zipcode", zipcode);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jsonObject;
    }

}

Java for Realm Android

//
//  APIResponse.java
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import org.json.*;
import io.realm.*;
import io.realm.annotations.*;


@RealmClass
public class APIResponse extends RealmObject{

    private Object message;
    private RealmList<Result> results;
    private int status;

    public void setMessage(Object message){
        this.message = message;
    }
    public Object getMessage(){
        return this.message;
    }
    public void setResults(RealmList<Result> results){
        this.results = results;
    }
    public RealmList<Result> getResults(){
        return this.results;
    }
    public void setStatus(int status){
        this.status = status;
    }
    public int getStatus(){
        return this.status;
    }

    /**
     * Creates instance using the passed realm and jsonObject to set the properties values
     */
    public static APIResponse fromJson(Realm realm, JSONObject jsonObject){
        if(jsonObject == null){
            return null;
        }
        APIResponse aPIResponse = realm.createObject(APIResponse.class);
        aPIResponse.message = jsonObject.opt("message");
        JSONArray resultsJsonArray = jsonObject.optJSONArray("results");
        if(resultsJsonArray != null){
            for (int i = 0; i < resultsJsonArray.length(); i++) {
                JSONObject resultsObject = resultsJsonArray.optJSONObject(i);
                Result resultsValue = Result.fromJson(realm, resultsObject);
                if(resultsValue != null){
                    aPIResponse.getResults().add(resultsValue);
                }
            }
        }
        aPIResponse.status = jsonObject.optInt("status");
        return aPIResponse;
    }

    /**
     * Returns all the available property values in the form of JSONObject instance where the key is the approperiate json key and the value is the value of the corresponding field
     */
    public static JSONObject toJsonObject(APIResponse aPIResponse)
    {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("message", aPIResponse.message);
            if(aPIResponse.results != null && aPIResponse.results.size() > 0){
                JSONArray resultsJsonArray = new JSONArray();
                for(Result resultsElement : aPIResponse.results){
                    resultsJsonArray.put(Result.toJsonObject(resultsElement));
                }
                jsonObject.put("results", resultsJsonArray);
            }
            jsonObject.put("status", aPIResponse.status);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jsonObject;
    }

}

//
//  Result.java
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import org.json.*;
import io.realm.*;
import io.realm.annotations.*;


@RealmClass
public class Result extends RealmObject{

    private String address1;
    private String address2;
    private String address3;
    private String kana1;
    private String kana2;
    private String kana3;
    private String prefcode;
    private String zipcode;

    public void setAddress1(String address1){
        this.address1 = address1;
    }
    public String getAddress1(){
        return this.address1;
    }
    public void setAddress2(String address2){
        this.address2 = address2;
    }
    public String getAddress2(){
        return this.address2;
    }
    public void setAddress3(String address3){
        this.address3 = address3;
    }
    public String getAddress3(){
        return this.address3;
    }
    public void setKana1(String kana1){
        this.kana1 = kana1;
    }
    public String getKana1(){
        return this.kana1;
    }
    public void setKana2(String kana2){
        this.kana2 = kana2;
    }
    public String getKana2(){
        return this.kana2;
    }
    public void setKana3(String kana3){
        this.kana3 = kana3;
    }
    public String getKana3(){
        return this.kana3;
    }
    public void setPrefcode(String prefcode){
        this.prefcode = prefcode;
    }
    public String getPrefcode(){
        return this.prefcode;
    }
    public void setZipcode(String zipcode){
        this.zipcode = zipcode;
    }
    public String getZipcode(){
        return this.zipcode;
    }

    /**
     * Creates instance using the passed realm and jsonObject to set the properties values
     */
    public static Result fromJson(Realm realm, JSONObject jsonObject){
        if(jsonObject == null){
            return null;
        }
        Result result = realm.createObject(Result.class);
        result.address1 = jsonObject.opt("address1");
        result.address2 = jsonObject.opt("address2");
        result.address3 = jsonObject.opt("address3");
        result.kana1 = jsonObject.opt("kana1");
        result.kana2 = jsonObject.opt("kana2");
        result.kana3 = jsonObject.opt("kana3");
        result.prefcode = jsonObject.opt("prefcode");
        result.zipcode = jsonObject.opt("zipcode");
        return result;
    }

    /**
     * Returns all the available property values in the form of JSONObject instance where the key is the approperiate json key and the value is the value of the corresponding field
     */
    public static JSONObject toJsonObject(Result result)
    {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("address1", result.address1);
            jsonObject.put("address2", result.address2);
            jsonObject.put("address3", result.address3);
            jsonObject.put("kana1", result.kana1);
            jsonObject.put("kana2", result.kana2);
            jsonObject.put("kana3", result.kana3);
            jsonObject.put("prefcode", result.prefcode);
            jsonObject.put("zipcode", result.zipcode);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jsonObject;
    }

}

GSON for Android

//
//  APIResponse.java
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import org.json.*;
import java.util.*;
import com.google.gson.annotations.SerializedName;


public class APIResponse{

    @SerializedName("message")
    private Object message;
    @SerializedName("results")
    private Result[] results;
    @SerializedName("status")
    private int status;

    public void setMessage(Object message){
        this.message = message;
    }
    public Object getMessage(){
        return this.message;
    }
    public void setResults(Result[] results){
        this.results = results;
    }
    public Result[] getResults(){
        return this.results;
    }
    public void setStatus(int status){
        this.status = status;
    }
    public int getStatus(){
        return this.status;
    }

    /**
     * Instantiate the instance using the passed jsonObject to set the properties values
     */
    public APIResponse(JSONObject jsonObject){
        if(jsonObject == null){
            return;
        }
        message = jsonObject.opt("message");
        JSONArray resultsJsonArray = jsonObject.optJSONArray("results");
        if(resultsJsonArray != null){
            ArrayList<Result> resultsArrayList = new ArrayList<>();
            for (int i = 0; i < resultsJsonArray.length(); i++) {
                JSONObject resultsObject = resultsJsonArray.optJSONObject(i);
                resultsArrayList.add(new Result(resultsObject));
            }
            results = (Result[]) resultsArrayList.toArray();
        }       status = jsonObject.optInt("status");
    }

    /**
     * Returns all the available property values in the form of JSONObject instance where the key is the approperiate json key and the value is the value of the corresponding field
     */
    public JSONObject toJsonObject()
    {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("message", message);
            if(results != null && results.length > 0){
                JSONArray resultsJsonArray = new JSONArray();
                for(Result resultsElement : results){
                    resultsJsonArray.put(resultsElement.toJsonObject());
                }
                jsonObject.put("results", resultsJsonArray);
            }
            jsonObject.put("status", status);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jsonObject;
    }

}

//
//  Result.java
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import org.json.*;
import java.util.*;
import com.google.gson.annotations.SerializedName;


public class Result{

    @SerializedName("address1")
    private String address1;
    @SerializedName("address2")
    private String address2;
    @SerializedName("address3")
    private String address3;
    @SerializedName("kana1")
    private String kana1;
    @SerializedName("kana2")
    private String kana2;
    @SerializedName("kana3")
    private String kana3;
    @SerializedName("prefcode")
    private String prefcode;
    @SerializedName("zipcode")
    private String zipcode;

    public void setAddress1(String address1){
        this.address1 = address1;
    }
    public String getAddress1(){
        return this.address1;
    }
    public void setAddress2(String address2){
        this.address2 = address2;
    }
    public String getAddress2(){
        return this.address2;
    }
    public void setAddress3(String address3){
        this.address3 = address3;
    }
    public String getAddress3(){
        return this.address3;
    }
    public void setKana1(String kana1){
        this.kana1 = kana1;
    }
    public String getKana1(){
        return this.kana1;
    }
    public void setKana2(String kana2){
        this.kana2 = kana2;
    }
    public String getKana2(){
        return this.kana2;
    }
    public void setKana3(String kana3){
        this.kana3 = kana3;
    }
    public String getKana3(){
        return this.kana3;
    }
    public void setPrefcode(String prefcode){
        this.prefcode = prefcode;
    }
    public String getPrefcode(){
        return this.prefcode;
    }
    public void setZipcode(String zipcode){
        this.zipcode = zipcode;
    }
    public String getZipcode(){
        return this.zipcode;
    }

    /**
     * Instantiate the instance using the passed jsonObject to set the properties values
     */
    public Result(JSONObject jsonObject){
        if(jsonObject == null){
            return;
        }
        address1 = jsonObject.opt("address1");
        address2 = jsonObject.opt("address2");
        address3 = jsonObject.opt("address3");
        kana1 = jsonObject.opt("kana1");
        kana2 = jsonObject.opt("kana2");
        kana3 = jsonObject.opt("kana3");
        prefcode = jsonObject.opt("prefcode");
        zipcode = jsonObject.opt("zipcode");
    }

    /**
     * Returns all the available property values in the form of JSONObject instance where the key is the approperiate json key and the value is the value of the corresponding field
     */
    public JSONObject toJsonObject()
    {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("address1", address1);
            jsonObject.put("address2", address2);
            jsonObject.put("address3", address3);
            jsonObject.put("kana1", kana1);
            jsonObject.put("kana2", kana2);
            jsonObject.put("kana3", kana3);
            jsonObject.put("prefcode", prefcode);
            jsonObject.put("zipcode", zipcode);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jsonObject;
    }

}

Swift Classes

//
//  APIResponse.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation


class APIResponse : NSObject, NSCoding{

    var message : AnyObject!
    var results : [Result]!
    var status : Int!


    /**
     * Instantiate the instance using the passed dictionary values to set the properties values
     */
    init(fromDictionary dictionary: [String:Any]){
        message = dictionary["message"] as? AnyObject
        results = [Result]()
        if let resultsArray = dictionary["results"] as? [[String:Any]]{
            for dic in resultsArray{
                let value = Result(fromDictionary: dic)
                results.append(value)
            }
        }
        status = dictionary["status"] as? Int
    }

    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if message != nil{
            dictionary["message"] = message
        }
        if results != nil{
            var dictionaryElements = [[String:Any]]()
            for resultsElement in results {
                dictionaryElements.append(resultsElement.toDictionary())
            }
            dictionary["results"] = dictionaryElements
        }
        if status != nil{
            dictionary["status"] = status
        }
        return dictionary
    }

    /**
    * NSCoding required initializer.
    * Fills the data from the passed decoder
    */
    @objc required init(coder aDecoder: NSCoder)
    {
         message = aDecoder.decodeObject(forKey: "message") as? AnyObject
         results = aDecoder.decodeObject(forKey :"results") as? [Result]
         status = aDecoder.decodeObject(forKey: "status") as? Int

    }

    /**
    * NSCoding required method.
    * Encodes mode properties into the decoder
    */
    @objc func encode(with aCoder: NSCoder)
    {
        if message != nil{
            aCoder.encode(message, forKey: "message")
        }
        if results != nil{
            aCoder.encode(results, forKey: "results")
        }
        if status != nil{
            aCoder.encode(status, forKey: "status")
        }

    }

}

//
//  Result.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation


class Result : NSObject, NSCoding{

    var address1 : String!
    var address2 : String!
    var address3 : String!
    var kana1 : String!
    var kana2 : String!
    var kana3 : String!
    var prefcode : String!
    var zipcode : String!


    /**
     * Instantiate the instance using the passed dictionary values to set the properties values
     */
    init(fromDictionary dictionary: [String:Any]){
        address1 = dictionary["address1"] as? String
        address2 = dictionary["address2"] as? String
        address3 = dictionary["address3"] as? String
        kana1 = dictionary["kana1"] as? String
        kana2 = dictionary["kana2"] as? String
        kana3 = dictionary["kana3"] as? String
        prefcode = dictionary["prefcode"] as? String
        zipcode = dictionary["zipcode"] as? String
    }

    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if address1 != nil{
            dictionary["address1"] = address1
        }
        if address2 != nil{
            dictionary["address2"] = address2
        }
        if address3 != nil{
            dictionary["address3"] = address3
        }
        if kana1 != nil{
            dictionary["kana1"] = kana1
        }
        if kana2 != nil{
            dictionary["kana2"] = kana2
        }
        if kana3 != nil{
            dictionary["kana3"] = kana3
        }
        if prefcode != nil{
            dictionary["prefcode"] = prefcode
        }
        if zipcode != nil{
            dictionary["zipcode"] = zipcode
        }
        return dictionary
    }

    /**
    * NSCoding required initializer.
    * Fills the data from the passed decoder
    */
    @objc required init(coder aDecoder: NSCoder)
    {
         address1 = aDecoder.decodeObject(forKey: "address1") as? String
         address2 = aDecoder.decodeObject(forKey: "address2") as? String
         address3 = aDecoder.decodeObject(forKey: "address3") as? String
         kana1 = aDecoder.decodeObject(forKey: "kana1") as? String
         kana2 = aDecoder.decodeObject(forKey: "kana2") as? String
         kana3 = aDecoder.decodeObject(forKey: "kana3") as? String
         prefcode = aDecoder.decodeObject(forKey: "prefcode") as? String
         zipcode = aDecoder.decodeObject(forKey: "zipcode") as? String

    }

    /**
    * NSCoding required method.
    * Encodes mode properties into the decoder
    */
    @objc func encode(with aCoder: NSCoder)
    {
        if address1 != nil{
            aCoder.encode(address1, forKey: "address1")
        }
        if address2 != nil{
            aCoder.encode(address2, forKey: "address2")
        }
        if address3 != nil{
            aCoder.encode(address3, forKey: "address3")
        }
        if kana1 != nil{
            aCoder.encode(kana1, forKey: "kana1")
        }
        if kana2 != nil{
            aCoder.encode(kana2, forKey: "kana2")
        }
        if kana3 != nil{
            aCoder.encode(kana3, forKey: "kana3")
        }
        if prefcode != nil{
            aCoder.encode(prefcode, forKey: "prefcode")
        }
        if zipcode != nil{
            aCoder.encode(zipcode, forKey: "zipcode")
        }

    }

}

Swift Classes for SwiftyJSON library

//
//  APIResponse.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation 
import SwiftyJSON


class APIResponse : NSObject, NSCoding{

    var message : AnyObject!
    var results : [Result]!
    var status : Int!


    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON!){
        if json.isEmpty{
            return
        }
        message = json["message"].stringValue
        results = [Result]()
        let resultsArray = json["results"].arrayValue
        for resultsJson in resultsArray{
            let value = Result(fromJson: resultsJson)
            results.append(value)
        }
        status = json["status"].intValue
    }

    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        let dictionary = [String:Any]()
        if message != nil{
            dictionary["message"] = message
        }
        if results != nil{
            var dictionaryElements = [[String:Any]]()
            for resultsElement in results {
                dictionaryElements.append(resultsElement.toDictionary())
            }
            dictionary["results"] = dictionaryElements
        }
        if status != nil{
            dictionary["status"] = status
        }
        return dictionary
    }

    /**
    * NSCoding required initializer.
    * Fills the data from the passed decoder
    */
    @objc required init(coder aDecoder: NSCoder)
    {
         message = aDecoder.decodeObject(forKey: "message") as? AnyObject
         results = aDecoder.decodeObject(forKey: "results") as? [Result]
         status = aDecoder.decodeObject(forKey: "status") as? Int

    }

    /**
    * NSCoding required method.
    * Encodes mode properties into the decoder
    */
    func encode(with aCoder: NSCoder)
    {
        if message != nil{
            aCoder.encode(message, forKey: "message")
        }
        if results != nil{
            aCoder.encode(results, forKey: "results")
        }
        if status != nil{
            aCoder.encode(status, forKey: "status")
        }

    }

}

//
//  Result.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation 
import SwiftyJSON


class Result : NSObject, NSCoding{

    var address1 : String!
    var address2 : String!
    var address3 : String!
    var kana1 : String!
    var kana2 : String!
    var kana3 : String!
    var prefcode : String!
    var zipcode : String!


    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON!){
        if json.isEmpty{
            return
        }
        address1 = json["address1"].stringValue
        address2 = json["address2"].stringValue
        address3 = json["address3"].stringValue
        kana1 = json["kana1"].stringValue
        kana2 = json["kana2"].stringValue
        kana3 = json["kana3"].stringValue
        prefcode = json["prefcode"].stringValue
        zipcode = json["zipcode"].stringValue
    }

    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        let dictionary = [String:Any]()
        if address1 != nil{
            dictionary["address1"] = address1
        }
        if address2 != nil{
            dictionary["address2"] = address2
        }
        if address3 != nil{
            dictionary["address3"] = address3
        }
        if kana1 != nil{
            dictionary["kana1"] = kana1
        }
        if kana2 != nil{
            dictionary["kana2"] = kana2
        }
        if kana3 != nil{
            dictionary["kana3"] = kana3
        }
        if prefcode != nil{
            dictionary["prefcode"] = prefcode
        }
        if zipcode != nil{
            dictionary["zipcode"] = zipcode
        }
        return dictionary
    }

    /**
    * NSCoding required initializer.
    * Fills the data from the passed decoder
    */
    @objc required init(coder aDecoder: NSCoder)
    {
         address1 = aDecoder.decodeObject(forKey: "address1") as? String
         address2 = aDecoder.decodeObject(forKey: "address2") as? String
         address3 = aDecoder.decodeObject(forKey: "address3") as? String
         kana1 = aDecoder.decodeObject(forKey: "kana1") as? String
         kana2 = aDecoder.decodeObject(forKey: "kana2") as? String
         kana3 = aDecoder.decodeObject(forKey: "kana3") as? String
         prefcode = aDecoder.decodeObject(forKey: "prefcode") as? String
         zipcode = aDecoder.decodeObject(forKey: "zipcode") as? String

    }

    /**
    * NSCoding required method.
    * Encodes mode properties into the decoder
    */
    func encode(with aCoder: NSCoder)
    {
        if address1 != nil{
            aCoder.encode(address1, forKey: "address1")
        }
        if address2 != nil{
            aCoder.encode(address2, forKey: "address2")
        }
        if address3 != nil{
            aCoder.encode(address3, forKey: "address3")
        }
        if kana1 != nil{
            aCoder.encode(kana1, forKey: "kana1")
        }
        if kana2 != nil{
            aCoder.encode(kana2, forKey: "kana2")
        }
        if kana3 != nil{
            aCoder.encode(kana3, forKey: "kana3")
        }
        if prefcode != nil{
            aCoder.encode(prefcode, forKey: "prefcode")
        }
        if zipcode != nil{
            aCoder.encode(zipcode, forKey: "zipcode")
        }

    }

}

Swift Classes for Realm

//
//  APIResponse.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport




class APIResponse: Object, NSCoding {

    dynamic var message: AnyObject!
    dynamic var results: List!
    dynamic var status: Int


    /**
     * Instantiate the instance using the passed dictionary values to set the properties values
     */
    class func fromDictionary(dictionary: [String:Any]) -> APIResponse   {
        let this = APIResponse()
        if let messageValue = dictionary["message"] as? AnyObject{
            this.message = messageValue
        }
        if let resultsArray = dictionary["results"] as? [[String:Any]]{
            var resultsItems = List(objectClassName: Result.className())
            for dic in resultsArray{
                let value = Result.fromDictionary(dic)
                resultsItems.addObject(value)
            }
            results = resultsItems
        }
        if let statusValue = dictionary["status"] as? Int{
            this.status = statusValue
        }
        return this
    }

    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if message != nil{
            dictionary["message"] = message
        }
        if results != nil{
            var dictionaryElements = [[String:Any]]()
            for i in 0 ..< results.count {
                if let resultsElement = results[i] as? Result{
                    dictionaryElements.append(resultsElement.toDictionary())
                }
            }
            dictionary["results"] = dictionaryElements
        }
        dictionary["status"] = status
        return dictionary
    }

    /**
    * NSCoding required initializer.
    * Fills the data from the passed decoder
    */
    @objc required init(coder aDecoder: NSCoder)
    {
         message = aDecoder.decodeObject(forKey: "message") as? AnyObject
         results = aDecoder.decodeObject(forKey: "results") as? List
         status = aDecoder.decodeObject(forKey: "status") as? Int

    }

    /**
    * NSCoding required method.
    * Encodes mode properties into the decoder
    */
    func encode(with aCoder: NSCoder)
    {
        if message != nil{
            aCoder.encode(message, forKey: "message")
        }
        if results != nil{
            aCoder.encode(results, forKey: "results")
        }
         status = aDecoder.decodeObject(forKey: "status") as? Int

    }

}

//
//  Result.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport




class Result: Object, NSCoding {

    dynamic var aPIResponse: APIResponse!
    dynamic var address1: String!
    dynamic var address2: String!
    dynamic var address3: String!
    dynamic var kana1: String!
    dynamic var kana2: String!
    dynamic var kana3: String!
    dynamic var prefcode: String!
    dynamic var zipcode: String!


    /**
     * Instantiate the instance using the passed dictionary values to set the properties values
     */
    class func fromDictionary(dictionary: [String:Any]) -> Result    {
        let this = Result()
        if let aPIResponseData = dictionary["aPIResponse"] as? [String:Any]{
            this.aPIResponse = APIResponse.fromDictionary(aPIResponseData)
        }
        if let address1Value = dictionary["address1"] as? String{
            this.address1 = address1Value
        }
        if let address2Value = dictionary["address2"] as? String{
            this.address2 = address2Value
        }
        if let address3Value = dictionary["address3"] as? String{
            this.address3 = address3Value
        }
        if let kana1Value = dictionary["kana1"] as? String{
            this.kana1 = kana1Value
        }
        if let kana2Value = dictionary["kana2"] as? String{
            this.kana2 = kana2Value
        }
        if let kana3Value = dictionary["kana3"] as? String{
            this.kana3 = kana3Value
        }
        if let prefcodeValue = dictionary["prefcode"] as? String{
            this.prefcode = prefcodeValue
        }
        if let zipcodeValue = dictionary["zipcode"] as? String{
            this.zipcode = zipcodeValue
        }
        return this
    }

    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if aPIResponse != nil{
            dictionary["aPIResponse"] = aPIResponse.toDictionary()
        }
        if address1 != nil{
            dictionary["address1"] = address1
        }
        if address2 != nil{
            dictionary["address2"] = address2
        }
        if address3 != nil{
            dictionary["address3"] = address3
        }
        if kana1 != nil{
            dictionary["kana1"] = kana1
        }
        if kana2 != nil{
            dictionary["kana2"] = kana2
        }
        if kana3 != nil{
            dictionary["kana3"] = kana3
        }
        if prefcode != nil{
            dictionary["prefcode"] = prefcode
        }
        if zipcode != nil{
            dictionary["zipcode"] = zipcode
        }
        return dictionary
    }

    /**
    * NSCoding required initializer.
    * Fills the data from the passed decoder
    */
    @objc required init(coder aDecoder: NSCoder)
    {
         aPIResponse = aDecoder.decodeObject(forKey: "aPIResponse") as? APIResponse
         address1 = aDecoder.decodeObject(forKey: "address1") as? String
         address2 = aDecoder.decodeObject(forKey: "address2") as? String
         address3 = aDecoder.decodeObject(forKey: "address3") as? String
         kana1 = aDecoder.decodeObject(forKey: "kana1") as? String
         kana2 = aDecoder.decodeObject(forKey: "kana2") as? String
         kana3 = aDecoder.decodeObject(forKey: "kana3") as? String
         prefcode = aDecoder.decodeObject(forKey: "prefcode") as? String
         zipcode = aDecoder.decodeObject(forKey: "zipcode") as? String

    }

    /**
    * NSCoding required method.
    * Encodes mode properties into the decoder
    */
    func encode(with aCoder: NSCoder)
    {
        if aPIResponse != nil{
            aCoder.encode(aPIResponse, forKey: "aPIResponse")
        }
        if address1 != nil{
            aCoder.encode(address1, forKey: "address1")
        }
        if address2 != nil{
            aCoder.encode(address2, forKey: "address2")
        }
        if address3 != nil{
            aCoder.encode(address3, forKey: "address3")
        }
        if kana1 != nil{
            aCoder.encode(kana1, forKey: "kana1")
        }
        if kana2 != nil{
            aCoder.encode(kana2, forKey: "kana2")
        }
        if kana3 != nil{
            aCoder.encode(kana3, forKey: "kana3")
        }
        if prefcode != nil{
            aCoder.encode(prefcode, forKey: "prefcode")
        }
        if zipcode != nil{
            aCoder.encode(zipcode, forKey: "zipcode")
        }

    }

}

Swift - CoreData

//
//  APIResponse.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation
import CoreData

class APIResponse : NSManagedObject{

    @NSManaged var message : AnyObject!
    @NSManaged var results : NSMutableSet!
    @NSManaged var status : Int


    /**
     * Instantiate the instance using the passed dictionary values to set the properties values
     */
    init(fromDictionary dictionary: [String:Any], context: NSManagedObjectContext)  {
        let entity = NSEntityDescription.entityForName("APIResponse", inManagedObjectContext: context)!
        super.init(entity: entity, insertIntoManagedObjectContext: context)
        if let messageValue = dictionary["message"] as? AnyObject{
            message = messageValue
        }
        if let resultsArray = dictionary["results"] as? [[String:Any]]{
            var resultsSet = NSMutableSet()
            for dic in resultsArray{
                let value = Result(fromDictionary: dic, context:context)
                resultsSet.addObject(value)
            }
            results = resultsSet
        }
        if let statusValue = dictionary["status"] as? Int{
            status = statusValue
        }
    }

    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if message != nil{
            dictionary["message"] = message
        }
        if results != nil{
            var dictionaryElements = [[String:Any]]()
            for resultsElement in results {
                dictionaryElements.append(resultsElement.toDictionary())
            }
            dictionary["results"] = dictionaryElements
        }
        dictionary["status"] = status
        return dictionary
    }

}

//
//  Result.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation
import CoreData

class Result : NSManagedObject{

    @NSManaged var aPIResponse : APIResponse!
    @NSManaged var address1 : String!
    @NSManaged var address2 : String!
    @NSManaged var address3 : String!
    @NSManaged var kana1 : String!
    @NSManaged var kana2 : String!
    @NSManaged var kana3 : String!
    @NSManaged var prefcode : String!
    @NSManaged var zipcode : String!


    /**
     * Instantiate the instance using the passed dictionary values to set the properties values
     */
    init(fromDictionary dictionary: [String:Any], context: NSManagedObjectContext)  {
        let entity = NSEntityDescription.entityForName("Result", inManagedObjectContext: context)!
        super.init(entity: entity, insertIntoManagedObjectContext: context)
        if let aPIResponseData = dictionary["aPIResponse"] as? [String:Any]{
            aPIResponse = APIResponse(fromDictionary: aPIResponseData, context:context)
        }
        if let address1Value = dictionary["address1"] as? String{
            address1 = address1Value
        }
        if let address2Value = dictionary["address2"] as? String{
            address2 = address2Value
        }
        if let address3Value = dictionary["address3"] as? String{
            address3 = address3Value
        }
        if let kana1Value = dictionary["kana1"] as? String{
            kana1 = kana1Value
        }
        if let kana2Value = dictionary["kana2"] as? String{
            kana2 = kana2Value
        }
        if let kana3Value = dictionary["kana3"] as? String{
            kana3 = kana3Value
        }
        if let prefcodeValue = dictionary["prefcode"] as? String{
            prefcode = prefcodeValue
        }
        if let zipcodeValue = dictionary["zipcode"] as? String{
            zipcode = zipcodeValue
        }
    }

    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if aPIResponse != nil{
            dictionary["aPIResponse"] = aPIResponse.toDictionary()
        }
        if address1 != nil{
            dictionary["address1"] = address1
        }
        if address2 != nil{
            dictionary["address2"] = address2
        }
        if address3 != nil{
            dictionary["address3"] = address3
        }
        if kana1 != nil{
            dictionary["kana1"] = kana1
        }
        if kana2 != nil{
            dictionary["kana2"] = kana2
        }
        if kana3 != nil{
            dictionary["kana3"] = kana3
        }
        if prefcode != nil{
            dictionary["prefcode"] = prefcode
        }
        if zipcode != nil{
            dictionary["zipcode"] = zipcode
        }
        return dictionary
    }

}

Swift Structures

//
//  APIResponse.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation

struct APIResponse{

    var message : AnyObject!
    var results : [Result]!
    var status : Int!


    /**
     * Instantiate the instance using the passed dictionary values to set the properties values
     */
    init(fromDictionary dictionary: [String:Any]){
        message = dictionary["message"] as? AnyObject
        results = [Result]()
        if let resultsArray = dictionary["results"] as? [[String:Any]]{
            for dic in resultsArray{
                let value = Result(fromDictionary: dic)
                results.append(value)
            }
        }
        status = dictionary["status"] as? Int
    }

    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if message != nil{
            dictionary["message"] = message
        }
        if results != nil{
            var dictionaryElements = [[String:Any]]()
            for resultsElement in results {
                dictionaryElements.append(resultsElement.toDictionary())
            }
            dictionary["results"] = dictionaryElements
        }
        if status != nil{
            dictionary["status"] = status
        }
        return dictionary
    }

}

//
//  Result.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation

struct Result{

    var address1 : String!
    var address2 : String!
    var address3 : String!
    var kana1 : String!
    var kana2 : String!
    var kana3 : String!
    var prefcode : String!
    var zipcode : String!


    /**
     * Instantiate the instance using the passed dictionary values to set the properties values
     */
    init(fromDictionary dictionary: [String:Any]){
        address1 = dictionary["address1"] as? String
        address2 = dictionary["address2"] as? String
        address3 = dictionary["address3"] as? String
        kana1 = dictionary["kana1"] as? String
        kana2 = dictionary["kana2"] as? String
        kana3 = dictionary["kana3"] as? String
        prefcode = dictionary["prefcode"] as? String
        zipcode = dictionary["zipcode"] as? String
    }

    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if address1 != nil{
            dictionary["address1"] = address1
        }
        if address2 != nil{
            dictionary["address2"] = address2
        }
        if address3 != nil{
            dictionary["address3"] = address3
        }
        if kana1 != nil{
            dictionary["kana1"] = kana1
        }
        if kana2 != nil{
            dictionary["kana2"] = kana2
        }
        if kana3 != nil{
            dictionary["kana3"] = kana3
        }
        if prefcode != nil{
            dictionary["prefcode"] = prefcode
        }
        if zipcode != nil{
            dictionary["zipcode"] = zipcode
        }
        return dictionary
    }

}

Swift Structures for Gloss

//
//  APIResponse.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

//  The "Swift - Struct - Gloss" support has been made available by CodeEagle
//  More about him/her can be found at his/her website: https://github.com/CodeEagle

import Foundation 
import Gloss

//MARK: - APIResponse
public struct APIResponse: Glossy {

    public let message : AnyObject!
    public let results : [Result]!
    public let status : Int!



    //MARK: Decodable
    public init?(json: JSON){
        message = "message" <~~ json
        results = "results" <~~ json
        status = "status" <~~ json
    }


    //MARK: Encodable
    public func toJSON() -> JSON? {
        return jsonify([
        "message" ~~> message,
        "results" ~~> results,
        "status" ~~> status,
        ])
    }

}

//
//  Result.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

//  The "Swift - Struct - Gloss" support has been made available by CodeEagle
//  More about him/her can be found at his/her website: https://github.com/CodeEagle

import Foundation 
import Gloss

//MARK: - Result
public struct Result: Glossy {

    public let address1 : String!
    public let address2 : String!
    public let address3 : String!
    public let kana1 : String!
    public let kana2 : String!
    public let kana3 : String!
    public let prefcode : String!
    public let zipcode : String!



    //MARK: Decodable
    public init?(json: JSON){
        address1 = "address1" <~~ json
        address2 = "address2" <~~ json
        address3 = "address3" <~~ json
        kana1 = "kana1" <~~ json
        kana2 = "kana2" <~~ json
        kana3 = "kana3" <~~ json
        prefcode = "prefcode" <~~ json
        zipcode = "zipcode" <~~ json
    }


    //MARK: Encodable
    public func toJSON() -> JSON? {
        return jsonify([
        "address1" ~~> address1,
        "address2" ~~> address2,
        "address3" ~~> address3,
        "kana1" ~~> kana1,
        "kana2" ~~> kana2,
        "kana3" ~~> kana3,
        "prefcode" ~~> prefcode,
        "zipcode" ~~> zipcode,
        ])
    }

}

Swift Mappable Classes for (Swift 3)

//
//  APIResponse.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation 
import ObjectMapper


class APIResponse : NSObject, NSCoding, Mappable{

    var message : AnyObject?
    var results : [Result]?
    var status : Int?


    class func newInstance(map: Map) -> Mappable?{
        return APIResponse()
    }
    required init?(map: Map){}
    private override init(){}

    func mapping(map: Map)
    {
        message <- map["message"]
        results <- map["results"]
        status <- map["status"]

    }

    /**
    * NSCoding required initializer.
    * Fills the data from the passed decoder
    */
    @objc required init(coder aDecoder: NSCoder)
    {
         message = aDecoder.decodeObject(forKey: "message") as? AnyObject
         results = aDecoder.decodeObject(forKey: "results") as? [Result]
         status = aDecoder.decodeObject(forKey: "status") as? Int

    }

    /**
    * NSCoding required method.
    * Encodes mode properties into the decoder
    */
    @objc func encode(with aCoder: NSCoder)
    {
        if message != nil{
            aCoder.encode(message, forKey: "message")
        }
        if results != nil{
            aCoder.encode(results, forKey: "results")
        }
        if status != nil{
            aCoder.encode(status, forKey: "status")
        }

    }

}

//
//  Result.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation 
import ObjectMapper


class Result : NSObject, NSCoding, Mappable{

    var address1 : String?
    var address2 : String?
    var address3 : String?
    var kana1 : String?
    var kana2 : String?
    var kana3 : String?
    var prefcode : String?
    var zipcode : String?


    class func newInstance(map: Map) -> Mappable?{
        return Result()
    }
    required init?(map: Map){}
    private override init(){}

    func mapping(map: Map)
    {
        address1 <- map["address1"]
        address2 <- map["address2"]
        address3 <- map["address3"]
        kana1 <- map["kana1"]
        kana2 <- map["kana2"]
        kana3 <- map["kana3"]
        prefcode <- map["prefcode"]
        zipcode <- map["zipcode"]

    }

    /**
    * NSCoding required initializer.
    * Fills the data from the passed decoder
    */
    @objc required init(coder aDecoder: NSCoder)
    {
         address1 = aDecoder.decodeObject(forKey: "address1") as? String
         address2 = aDecoder.decodeObject(forKey: "address2") as? String
         address3 = aDecoder.decodeObject(forKey: "address3") as? String
         kana1 = aDecoder.decodeObject(forKey: "kana1") as? String
         kana2 = aDecoder.decodeObject(forKey: "kana2") as? String
         kana3 = aDecoder.decodeObject(forKey: "kana3") as? String
         prefcode = aDecoder.decodeObject(forKey: "prefcode") as? String
         zipcode = aDecoder.decodeObject(forKey: "zipcode") as? String

    }

    /**
    * NSCoding required method.
    * Encodes mode properties into the decoder
    */
    @objc func encode(with aCoder: NSCoder)
    {
        if address1 != nil{
            aCoder.encode(address1, forKey: "address1")
        }
        if address2 != nil{
            aCoder.encode(address2, forKey: "address2")
        }
        if address3 != nil{
            aCoder.encode(address3, forKey: "address3")
        }
        if kana1 != nil{
            aCoder.encode(kana1, forKey: "kana1")
        }
        if kana2 != nil{
            aCoder.encode(kana2, forKey: "kana2")
        }
        if kana3 != nil{
            aCoder.encode(kana3, forKey: "kana3")
        }
        if prefcode != nil{
            aCoder.encode(prefcode, forKey: "prefcode")
        }
        if zipcode != nil{
            aCoder.encode(zipcode, forKey: "zipcode")
        }

    }

}

Swift Structures for Unbox

//
//  APIResponse.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation
import Unbox

struct APIResponse: Unboxable {

    let message : AnyObject!
    let results : [Result]!
    let status : Int!


    init(unboxer: Unboxer) throws {
        message = try unboxer.unbox(key: "message")
        results = try unboxer.unbox(key: "results")
        status = try unboxer.unbox(key: "status")
    }

    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if message != nil{
            dictionary["message"] = message
        }
        if results != nil{
            var dictionaryElements = [[String:Any]]()
            for resultsElement in results {
                dictionaryElements.append(resultsElement.toDictionary())
            }
            dictionary["results"] = dictionaryElements
        }
        if status != nil{
            dictionary["status"] = status
        }
        return dictionary
    }

}

//
//  Result.swift
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation
import Unbox

struct Result: Unboxable {

    let address1 : String!
    let address2 : String!
    let address3 : String!
    let kana1 : String!
    let kana2 : String!
    let kana3 : String!
    let prefcode : String!
    let zipcode : String!


    init(unboxer: Unboxer) throws {
        address1 = try unboxer.unbox(key: "address1")
        address2 = try unboxer.unbox(key: "address2")
        address3 = try unboxer.unbox(key: "address3")
        kana1 = try unboxer.unbox(key: "kana1")
        kana2 = try unboxer.unbox(key: "kana2")
        kana3 = try unboxer.unbox(key: "kana3")
        prefcode = try unboxer.unbox(key: "prefcode")
        zipcode = try unboxer.unbox(key: "zipcode")
    }

    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if address1 != nil{
            dictionary["address1"] = address1
        }
        if address2 != nil{
            dictionary["address2"] = address2
        }
        if address3 != nil{
            dictionary["address3"] = address3
        }
        if kana1 != nil{
            dictionary["kana1"] = kana1
        }
        if kana2 != nil{
            dictionary["kana2"] = kana2
        }
        if kana3 != nil{
            dictionary["kana3"] = kana3
        }
        if prefcode != nil{
            dictionary["prefcode"] = prefcode
        }
        if zipcode != nil{
            dictionary["zipcode"] = zipcode
        }
        return dictionary
    }

}

Objective-C - iOS

//
//  APIResponse.h
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//

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

#import <UIKit/UIKit.h>
#import "Result.h"

@interface APIResponse : NSObject

@property (nonatomic, strong) NSObject * message;
@property (nonatomic, strong) NSArray * results;
@property (nonatomic, assign) NSInteger status;

-(instancetype)initWithDictionary:(NSDictionary *)dictionary;

-(NSDictionary *)toDictionary;
@end

//
//  APIResponse.m
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport



#import "APIResponse.h"

NSString *const kAPIResponseMessage = @"message";
NSString *const kAPIResponseResults = @"results";
NSString *const kAPIResponseStatus = @"status";

@interface APIResponse ()
@end
@implementation APIResponse




/**
 * Instantiate the instance using the passed dictionary values to set the properties values
 */

-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
    self = [super init];
    if(![dictionary[kAPIResponseMessage] isKindOfClass:[NSNull class]]){
        self.message = dictionary[kAPIResponseMessage];
    }   
    if(dictionary[kAPIResponseResults] != nil && [dictionary[kAPIResponseResults] isKindOfClass:[NSArray class]]){
        NSArray * resultsDictionaries = dictionary[kAPIResponseResults];
        NSMutableArray * resultsItems = [NSMutableArray array];
        for(NSDictionary * resultsDictionary in resultsDictionaries){
            Result * resultsItem = [[Result alloc] initWithDictionary:resultsDictionary];
            [resultsItems addObject:resultsItem];
        }
        self.results = resultsItems;
    }
    if(![dictionary[kAPIResponseStatus] isKindOfClass:[NSNull class]]){
        self.status = [dictionary[kAPIResponseStatus] integerValue];
    }

    return self;
}


/**
 * 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
 */
-(NSDictionary *)toDictionary
{
    NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
    if(self.message != nil){
        dictionary[kAPIResponseMessage] = self.message;
    }
    if(self.results != nil){
        NSMutableArray * dictionaryElements = [NSMutableArray array];
        for(Result * resultsElement in self.results){
            [dictionaryElements addObject:[resultsElement toDictionary]];
        }
        dictionary[kAPIResponseResults] = dictionaryElements;
    }
    dictionary[kAPIResponseStatus] = @(self.status);
    return dictionary;

}

/**
 * Implementation of NSCoding encoding method
 */
/**
 * 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
 */
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    if(self.message != nil){
        [aCoder encodeObject:self.message forKey:kAPIResponseMessage];
    }
    if(self.results != nil){
        [aCoder encodeObject:self.results forKey:kAPIResponseResults];
    }
    [aCoder encodeObject:@(self.status) forKey:kAPIResponseStatus];
}

/**
 * Implementation of NSCoding initWithCoder: method
 */
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    self.message = [aDecoder decodeObjectForKey:kAPIResponseMessage];
    self.results = [aDecoder decodeObjectForKey:kAPIResponseResults];
    self.status = [[aDecoder decodeObjectForKey:kAPIResponseStatus] integerValue];
    return self;

}

/**
 * Implementation of NSCopying copyWithZone: method
 */
- (instancetype)copyWithZone:(NSZone *)zone
{
    APIResponse *copy = [APIResponse new];

    copy.message = [self.message copy];
    copy.results = [self.results copy];
    copy.status = self.status;

    return copy;
}
@end

//
//  Result.h
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//

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

#import <UIKit/UIKit.h>

@interface Result : NSObject

@property (nonatomic, strong) NSString * address1;
@property (nonatomic, strong) NSString * address2;
@property (nonatomic, strong) NSString * address3;
@property (nonatomic, strong) NSString * kana1;
@property (nonatomic, strong) NSString * kana2;
@property (nonatomic, strong) NSString * kana3;
@property (nonatomic, strong) NSString * prefcode;
@property (nonatomic, strong) NSString * zipcode;

-(instancetype)initWithDictionary:(NSDictionary *)dictionary;

-(NSDictionary *)toDictionary;
@end

Objective-C - MAC

//
//  APIResponse.h
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//

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

#import <AppKit/AppKit.h>
#import "Result.h"

@interface APIResponse : NSObject

@property (nonatomic, strong) NSObject * message;
@property (nonatomic, strong) NSArray * results;
@property (nonatomic, assign) NSInteger status;

-(instancetype)initWithDictionary:(NSDictionary *)dictionary;

-(NSDictionary *)toDictionary;
@end

//
//  APIResponse.m
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport



#import "APIResponse.h"

NSString *const kAPIResponseMessage = @"message";
NSString *const kAPIResponseResults = @"results";
NSString *const kAPIResponseStatus = @"status";

@interface APIResponse ()
@end
@implementation APIResponse




/**
 * Instantiate the instance using the passed dictionary values to set the properties values
 */

-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
    self = [super init];
    if(![dictionary[kAPIResponseMessage] isKindOfClass:[NSNull class]]){
        self.message = dictionary[kAPIResponseMessage];
    }

    if(dictionary[kAPIResponseResults] != nil && [dictionary[kAPIResponseResults] isKindOfClass:[NSArray class]]){
        NSArray * resultsDictionaries = dictionary[kAPIResponseResults];
        NSMutableArray * resultsItems = [NSMutableArray array];
        for(NSDictionary * resultsDictionary in resultsDictionaries){
            Result * resultsItem = [[Result alloc] initWithDictionary:resultsDictionary];
            [resultsItems addObject:resultsItem];
        }
        self.results = resultsItems;
    }
    if(![dictionary[kAPIResponseStatus] isKindOfClass:[NSNull class]]){
        self.status = [dictionary[kAPIResponseStatus] integerValue];
    }

    return self;
}


/**
 * 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
 */
-(NSDictionary *)toDictionary
{
    NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
    if(self.message != nil){
        dictionary[kAPIResponseMessage] = self.message;
    }
    if(self.results != nil){
        NSMutableArray * dictionaryElements = [NSMutableArray array];
        for(Result * resultsElement in self.results){
            [dictionaryElements addObject:[resultsElement toDictionary]];
        }
        dictionary[kAPIResponseResults] = dictionaryElements;
    }
    dictionary[kAPIResponseStatus] = @(self.status);
    return dictionary;

}

/**
 * Implementation of NSCoding encoding method
 */
/**
 * 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
 */
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    if(self.message != nil){
        [aCoder encodeObject:self.message forKey:kAPIResponseMessage];
    }
    if(self.results != nil){
        [aCoder encodeObject:self.results forKey:kAPIResponseResults];
    }
    [aCoder encodeObject:@(self.status) forKey:kAPIResponseStatus];
}

/**
 * Implementation of NSCoding initWithCoder: method
 */
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    self.message = [aDecoder decodeObjectForKey:kAPIResponseMessage];
    self.results = [aDecoder decodeObjectForKey:kAPIResponseResults];
    self.status = [[aDecoder decodeObjectForKey:kAPIResponseStatus] integerValue];
    return self;

}

/**
 * Implementation of NSCopying copyWithZone: method
 */
- (instancetype)copyWithZone:(NSZone *)zone
{
    APIResponse *copy = [APIResponse new];

    copy.message = [self.message copy];
    copy.results = [self.results copy];
    copy.status = self.status;

    return copy;
}
@end

//
//  Result.h
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//

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

#import <AppKit/AppKit.h>

@interface Result : NSObject

@property (nonatomic, strong) NSString * address1;
@property (nonatomic, strong) NSString * address2;
@property (nonatomic, strong) NSString * address3;
@property (nonatomic, strong) NSString * kana1;
@property (nonatomic, strong) NSString * kana2;
@property (nonatomic, strong) NSString * kana3;
@property (nonatomic, strong) NSString * prefcode;
@property (nonatomic, strong) NSString * zipcode;

-(instancetype)initWithDictionary:(NSDictionary *)dictionary;

-(NSDictionary *)toDictionary;
@end

Objective-C - CoreData

<br />//
//  APIResponse.h
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//

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

#import <CoreData/CoreData.h>
@class Result;

@interface APIResponse : NSManagedObject

@property (nonatomic, strong) NSObject * message;
@property (nonatomic, strong) NSSet * results;
@property (nonatomic, assign) NSInteger status;

-(instancetype)initWithDictionary:(NSDictionary *)dictionary context:(NSManagedObjectContext *)context;

-(NSDictionary *)toDictionary;
@end

//
//  APIResponse.m
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport



#import "APIResponse.h"
#import "Result.h"

@interface APIResponse ()
@end
@implementation APIResponse

@dynamic message;
@dynamic results;
@dynamic status;



/**
 * Instantiate the instance using the passed dictionary values to set the properties values
 */

-(instancetype)initWithDictionary:(NSDictionary *)dictionary context:(NSManagedObjectContext *)context
{
    if(dictionary == nil || [dictionary isKindOfClass:[NSNull class]]){
        return nil;
    }
    NSEntityDescription * entityDescription = [NSEntityDescription entityForName:@"APIResponse" inManagedObjectContext:context];
    self = [super initWithEntity:entityDescription insertIntoManagedObjectContext:context];

    if(![dictionary[@"message"] isKindOfClass:[NSNull class]]){
        self.message = dictionary[@"message"];
    }

    if(dictionary[@"results"] != nil && [dictionary[@"results"] isKindOfClass:[NSNull class]]){
        NSArray * resultsDictionaries = dictionary[@"results"];
        NSMutableSet * resultsItems = [NSMutableSet set];
        for(NSDictionary * resultsDictionary in resultsDictionaries){
            Result * resultsItem = [[Result alloc] initWithDictionary:resultsDictionary context:context];
            [resultsItems addObject:resultsItem];
        }
        self.results = resultsItems;
    }
    if(![dictionary[@"status"] isKindOfClass:[NSNull class]]){
        self.status = [dictionary[@"status"] integerValue];
    }

    return self;
}


/**
 * 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
 */
-(NSDictionary *)toDictionary
{
    NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
    if(self.message != nil){
        dictionary[@"message"] = self.message;
    }
    if(self.results != nil){
        NSMutableArray * dictionaryElements = [NSMutableArray array];
        for(Result * resultsElement in self.results){
            [dictionaryElements addObject:[resultsElement toDictionary]];
        }
        dictionary[@"results"] = dictionaryElements;
    }
    dictionary[@"status"] = @(self.status);
    return dictionary;

}
@end

//
//  Result.h
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//

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

#import <CoreData/CoreData.h>
@class APIResponse;

@interface Result : NSManagedObject

@property (nonatomic, strong) APIResponse * aPIResponse;
@property (nonatomic, strong) NSString * address1;
@property (nonatomic, strong) NSString * address2;
@property (nonatomic, strong) NSString * address3;
@property (nonatomic, strong) NSString * kana1;
@property (nonatomic, strong) NSString * kana2;
@property (nonatomic, strong) NSString * kana3;
@property (nonatomic, strong) NSString * prefcode;
@property (nonatomic, strong) NSString * zipcode;

-(instancetype)initWithDictionary:(NSDictionary *)dictionary context:(NSManagedObjectContext *)context;

-(NSDictionary *)toDictionary;
@end

Objective-C for Realm iOS

//
//  APIResponse.h
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//

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

#import <Realm/Realm.h>
RLM_ARRAY_TYPE(Result)
@class Result;

@interface APIResponse : RLMObject

@property (nonatomic, strong) NSObject * message;
@property (nonatomic, strong) RLMArray<Result> * results;
@property (nonatomic, assign) NSInteger status;

-(instancetype)initWithDictionary:(NSDictionary *)dictionary;

-(NSDictionary *)toDictionary;
@end

//
//  APIResponse.m
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//  Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport



#import "APIResponse.h"
#import "Result.h"

@interface APIResponse ()
@end
@implementation APIResponse




/**
 * Instantiate the instance using the passed dictionary values to set the properties values
 */

-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
    if(dictionary == nil || [dictionary isKindOfClass:[NSNull class]]){
        return nil;
    }
    self = [super init];

    if(![dictionary[@"message"] isKindOfClass:[NSNull class]]){
        self.message = dictionary[@"message"];
    }

    if(dictionary[@"results"] != nil && ![dictionary[@"results"] isKindOfClass:[NSNull class]]){
        NSArray * resultsDictionaries = dictionary[@"results"];
        RLMArray * resultsItems = [[RLMArray alloc] initWithObjectClassName:[Result className]];
        for(NSDictionary * resultsDictionary in resultsDictionaries){
            Result * resultsItem = [[Result alloc] initWithDictionary:resultsDictionary];
            [resultsItems addObject:resultsItem];
        }
        self.results = (RLMArray<Result> *)resultsItems;
    }
    if(![dictionary[@"status"] isKindOfClass:[NSNull class]]){
        self.status = [dictionary[@"status"] integerValue];
    }

    return self;
}


/**
 * 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
 */
-(NSDictionary *)toDictionary
{
    NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
    if(self.message != nil){
        dictionary[@"message"] = self.message;
    }
    if(self.results != nil){
        NSMutableArray * dictionaryElements = [NSMutableArray array];
        for(Result * resultsElement in self.results){
            [dictionaryElements addObject:[resultsElement toDictionary]];
        }
        dictionary[@"results"] = dictionaryElements;
    }
    dictionary[@"status"] = @(self.status);
    return dictionary;

}

/**
 * Implementation of NSCoding encoding method
 */
/**
 * 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
 */
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    if(self.message != nil){
        [aCoder encodeObject:self.message forKey:@"message"];
    }
    if(self.results != nil){
        [aCoder encodeObject:self.results forKey:@"results"];
    }
    [aCoder encodeObject:@(self.status) forKey:@"status"];
}

/**
 * Implementation of NSCoding initWithCoder: method
 */
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    self.message = [aDecoder decodeObjectForKey:@"message"];
    self.results = [aDecoder decodeObjectForKey:@"results"];
    self.status = [[aDecoder decodeObjectForKey:@"status"] integerValue];
    return self;

}
@end

//
//  Result.h
//
//  Create by kato jun on 6/7/2017
//  Copyright © 2017. All rights reserved.
//

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

#import <Realm/Realm.h>
RLM_ARRAY_TYPE(APIResponse)
@class APIResponse;

@interface Result : RLMObject

@property (nonatomic, strong) APIResponse * aPIResponse;
@property (nonatomic, strong) NSString * address1;
@property (nonatomic, strong) NSString * address2;
@property (nonatomic, strong) NSString * address3;
@property (nonatomic, strong) NSString * kana1;
@property (nonatomic, strong) NSString * kana2;
@property (nonatomic, strong) NSString * kana3;
@property (nonatomic, strong) NSString * prefcode;
@property (nonatomic, strong) NSString * zipcode;

-(instancetype)initWithDictionary:(NSDictionary *)dictionary;

-(NSDictionary *)toDictionary;
@end

おわりに

JSONExportを使ってJSONからモデルクラスを自動生成する方法をご紹介しました。生成されるモデルの内容について賛否はあるかと思います。(例えばSwiftのclassやstructのプロパティがimplicitly unwrapped optionalになっていたりする部分)
しかしそれでも項目が多いJSONなどは自分でイチからモデルクラスを作るよりはJSONExportを使った方が実装が早いんじゃないかなと思います。ある種雛形として使う形で。

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.