0

I have the following JSON, which I am using together with ObjectMapper:

Open Api

Response snippet

{
  "data": [
    {
      "CategoryName": "רוגע",
      "CategoryID": "63",
      "CategoryDate": "2016-08-26 02:12:05",
      "CategoryImage": "relax.png",
      "SubCategoryArray": [
        {
          "SubCategoryName": "רוגע",
          "SubCategoryRefID": "63",
          "SubCategoryID": "86",
          "SubCategoryDate": "2016-08-28 02:57:07",
          "TextArray": [
            {
              "TextID": "32",
              "Text": "<p dir=\"rtl\"><span style=\"font-size:48px\"><strong><span dir=\"RTL\" lang=\"HE\" style=\"font-family:Arial\">פרופורציה</span></strong> . הכול הבל הבלים. חולף כהרף עין. אז לנשום.</span></p>\r\n"
            }, 

My problem is getting the data from "SubCategoryArray", and "TextArray"

I tried to do the following in my mapping:

import UIKit
import ObjectMapper

class APIResult: Mappable {
    var data : [dataArray]?
    required init?(map: Map){
    }
    func mapping(map: Map) {

        data <- map["data"]
    }
}
class dataArray: Mappable{
    var CategoryName: String?
     var CategoryID: String?
     var CategoryDate: String?
     var CategoryImage: String?
    var SubCategoryArray: SubCategoryArray?
    required init?(map: Map){
    }
    func mapping(map: Map) {

        CategoryName <- map["CategoryName"]
        CategoryID <- map["CategoryID"]
        CategoryDate <- map["CategoryDate"]
        CategoryImage <- map["CategoryImage"]
        SubCategoryArray <- map["SubCategoryArray"]
    }    
}
class SubCategoryArray: Mappable {
    var SubCategoryName: String?
    var SubCategoryRefID: String?
    var SubCategoryID: String?
    var SubCategoryDate: String?
    var TextArray: TextArray?
    required init?(map: Map){
    }
    func mapping(map: Map) {

        SubCategoryName <- map["SubCategoryName"]
        SubCategoryRefID <- map["SubCategoryRefID"]
        SubCategoryID <- map["SubCategoryID"]
        SubCategoryDate <- map["SubCategoryDate"]
        TextArray <-  map["TextArray"]
    }
}
class TextArray: Mappable {
    var TextID: String?
    var Text:String?
    required init?(map: Map){
    }
    func mapping(map: Map) {

        TextID <- map["TextID"]
        Text <- map["Text"]
//        SubCategoryID <- map["SubCategoryID"]
//        SubCategoryDate <- map["SubCategoryDate"]
//        TextArray <-  map["TextArray"]
    }
}

Please point what I am doing wrong.

2
  • as per my understanding all is good, but you are using dataArray class on top and you json response show only data , that can be the only problem. Commented Jan 17, 2017 at 6:59
  • Actually it is parsing data within data but it does not the lower levels like SubCategoryArray and TextArray Commented Jan 17, 2017 at 7:08

1 Answer 1

1

This is how you would map this data

import Foundation
import ObjectMapper

class customData: Mappable {


var categoryName: String = ""
var categoryId: String = ""
var subCategoryArray: [SubCategoryArray] = []

required init?(_ map: Map) {

}

func mapping(map: Map) {

    categoryName            <- map["data.CategoryName"]
    categoryId              <- map["data.CategoryID"]
    subCategoryArray        <- map["data.SubCategoryArray"]
}
}

class SubCategoryArray: Mappable {


var SubCategoryName: String = ""
var SubCategoryRefID: String = ""

var textArray: [TextArray] = []
required init?(_ map: Map) {

}

func mapping(map: Map) {

    SubCategoryName         <- map["SubCategoryName"]
    SubCategoryRefID        <- map["SubCategoryRefID"]
    textArray               <- map["TextArray"]

}
}

class TextArray: Mappable {


var TextID: String = ""
var Text: String = ""

required init?(_ map: Map) {

}

func mapping(map: Map) {

    TextID              <- map["TextID"]
    Text                <- map["Text"]
}
}

Let me know if you find any difficulty.

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

Comments

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.