0

I am trying to extract a key value from my response using SwiftHTTP library. The following is my response:

  {
    "statusCode": 200,
    "statusMessage": "Success",
    "data": {
    "TotalCount": 3,
    "GetHomeHistory": [
       {
       "GroupType": "3",
       "GroupName": "name2",
       "MessageId": 1829,
       "ConversationId": 46,
       "DeliveryTimeStamp": "4/6/2017 4:38:12 AM",
       "MessageTranscationId": "3049",
       "MessageText": "SGVsbG8=",
       "Unread_Message_Count": 24,
       "UserId": 69
     },
     {
       "GroupType": "2",
       "GroupName": "name2,name1",
       "MessageId": 1754,
       "ConversationId": 47,
       "DeliveryTimeStamp": "3/4/2017 7:01:17 AM",
       "MessageTranscationId": "2932",
       "MessageText": "UnRncg==",
       "Unread_Message_Count": 4,
       "UserId": 69
     },
     {
       "GroupType": "1",
       "GroupName": "name3",
       "MessageId": 1753,
       "ConversationId": 49,
       "DeliveryTimeStamp": "2/28/2017 10:23:24 AM",
       "MessageTranscationId": "2931",
       "MessageText": "UnRncg==",
       "Unread_Message_Count": 1,
       "UserId": 69
     }
     ]
    }
   }

I am trying to extract values from "GetHomeHistory" key. The link for the library is as follows JSONJoy SwiftHTTP Library

I am calling in the following way:

struct HomeModel : JSONJoy {
let status:Int
let message:String
let totalCount: Int
var conversationHistoryArr: [ConversationHistoryModel]? =  [ConversationHistoryModel] ()


init(_ decoder: JSONDecoder) throws {

    status = try decoder["statusCode"].getInt()
    message = try decoder["statusMessage"].getString()
    if let respData = decoder["data"].dictionary  {
        if respData["TotalCount"]?.getInt() != nil {
            totalCount = (respData["TotalCount"]?.getInt())!
        } else {
            totalCount = 0
        }
        if respData["GetHomeHistory"]?.getArray(&conversationHistoryArr) != nil{

            if let homeHistoryData = decoder["GetHomeHistory"].array{
                for rsp in homeHistoryData {
                    let resp = try?  ConversationHistoryModel(rsp)
                    print(resp as Any)
                    conversationHistoryArr?.append(resp!)
                }
            }
            else{
                conversationHistoryArr = [ConversationHistoryModel]()
            }

        }


    }
    else{
        totalCount = 0
    }
}

}

The "ConversationHistoryModel" is as follows :

  struct ConversationHistoryModel : JSONJoy {

let groupType:String
let groupName:String
let messageId:Int
let conversationId:Int
let messageTime:String
let messageTransactionId:String
let messageText:String
let unreadMessageCount:Int
let userId:Int

init(_ respData: JSONDecoder) throws {
    if respData["GroupType"].getString() != nil{
        groupType = (respData["GroupType"].getString())!
    }
    else{
        groupType = ""
    }
    if respData["GroupName"].getString() != nil {
        groupName = (respData["GroupName"].getString())!
    } else {
        groupName = ""
    }
    if respData["MessageId"].getInt() != nil {
        messageId = (respData["MessageId"].getInt())!
    } else {
        messageId = 0
    }
    if respData["ConversationId"].getInt() != nil {
        conversationId = (respData["ConversationId"].getInt())!
    } else {
        conversationId = 0
    }
    if respData["DeliveryTimeStamp"].getString() != nil {
        messageTime = (respData["DeliveryTimeStamp"].getString())!
    } else {
        messageTime = ""
    }
    if respData["MessageTranscationId"].getString() != nil {
        messageTransactionId = (respData["MessageTranscationId"].getString())!
    } else {
        messageTransactionId = ""
    }
    if respData["MessageText"].getString() != nil {
        messageText = (respData["MessageText"].getString())!
    } else {
        messageText = ""
    }
    if respData["Unread_Message_Count"].getInt() != nil {
        unreadMessageCount = (respData["Unread_Message_Count"].getInt())!
    } else {
        unreadMessageCount = 0
    }

    if respData["UserId"].getInt() != nil {
        userId = (respData["UserId"].getInt())!
    } else {
        userId = 0
    }


}
}

Please help me in extracting that value.

5
  • your "GetHomeHistory" is array on NSDictionary objects. Commented Apr 6, 2017 at 7:25
  • @TusharSharma yes.. can you help me in extracting those...? Commented Apr 6, 2017 at 7:36
  • Alternatively you can use Object Mapper. Sample usage can be found Here Commented Apr 6, 2017 at 7:42
  • @UmairAfzal its again addition of another library which i don't want to do there is a method in JSONJoy but i am not getting a proper way to use it Commented Apr 6, 2017 at 8:59
  • Well the reason of recommending ObjectMaper is that you do not have to add all that nil checks, And you can remove JSONJoy and use ObjectMapper Commented Apr 6, 2017 at 9:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.