-4
{"dataList":{"1547795650562": {
        "c0a8007b-6759-111d-8167-59e8dabe0086": {
          "recordDate": 1547795650562,
          "resultValue": "160",
          "vitalParameter": {
            "uom": {
              "code": "KG",
              "name": "KG",
              "id": "c0a8007b-6759-111d-8167-59e76204007f"
            },
            "resultType": {
              "code": "VSRTNUMERIC",
              "name": "Numeric",
              "id": "20cf4756-40b0-4cc1-acb5-861765370a41"
            },
            "code": "29463-7",
            "name": "Weight",
            "id": "c0a8007b-6759-111d-8167-59e8dabe0086"
          },
          "id": "c0a8007b-6855-1d16-8168-5fd18fa301b7"
        }}
}}

getting 1547795650562 and c0a8007b-6759-111d-8167-59e8dabe0086 as class names. But I dont want like this;

class DataList : NSObject, NSCoding{
    var 1547795650562 : 1547795650562!
}

class 1547795650562 : NSObject, NSCoding{
    var c0a8007b6759111d816759e8dabe0086 : VitalParameter!
}

But the problem here is, 1547795650562 and c0a8007b-6759-111d-8167-59e8dabe0086 cannot be hard coded because they may change.

c0a8007b-6759-111d-8167-59e8dabe0086 is dynamic id and 1547795650562 is recordDate. Inner object is repetitive.

But I have to map as the keys are of recordDate and id respectively.

halfer
  • 19,824
  • 17
  • 99
  • 186
mrafi5
  • 35
  • 8
  • 1
    Have you tried anything so far? Also, what all data do you need from this JSON? – PGDev Nov 01 '19 at 05:44
  • ys. But getting 1547795650562 and c0a8007b-6759-111d-8167-59e8dabe0086 as class names. But I have to map as the keys are of recordDate and id respectively. Thanks – mrafi5 Nov 01 '19 at 05:46
  • Could not understand. Please elaborate. – PGDev Nov 01 '19 at 05:47
  • Can you add some code which give you class and how you are reading this JSON? base don that we can help you in your Modal creation issue. – CodeChanger Nov 01 '19 at 05:48
  • class DataList : NSObject, NSCoding{ var 1547795650562 : 1547795650562! – mrafi5 Nov 01 '19 at 05:48
  • class 1547795650562 : NSObject, NSCoding{ var c0a8007b6759111d816759e8dabe0086 : VitalParameter! – mrafi5 Nov 01 '19 at 05:48
  • But the problem here is , 1547795650562 and c0a8007b-6759-111d-8167-59e8dabe0086 cannot be hard coded because they may change – mrafi5 Nov 01 '19 at 05:54
  • c0a8007b-6759-111d-8167-59e8dabe0086 is dynamic id and 1547795650562 is recordDate. Inner object is repeatative – mrafi5 Nov 01 '19 at 05:55
  • who is managing backend? – Shahrukh Nov 01 '19 at 06:04
  • I see a major design problem with this JSON, those dynamic ids must be values, not keys. You can maybe solve this by using reflection or other methods but really this is a backend problem. Those ids must be wrapped in static named keys. Tell your backend guys to fix it, this is not the app developers problem. – Batuhan C. Nov 01 '19 at 06:23
  • This question is highly downvoted, abandoned, and has left some questions unanswered. On that basis I think it should be closed. "Lacks Clarity" is probably recommended - it isn't going to get fixed now. – halfer Dec 21 '22 at 15:58

1 Answers1

0

Try using Codable instead of NSCoding to parse your JSON data.

Models:

struct Root: Codable {
    let dataList: [String:[String:Record]]
}

struct Record: Codable {
    let recordDate: Int
    let resultValue: String
    let vitalParameter: VitalParameter
    let id: String
}

struct VitalParameter: Codable {
    let uom, resultType: ResultType
    let code, name, id: String
}

struct ResultType: Codable {
    let code, name, id: String
}

Parse the JSON data using above models like,

do {
    let response = try JSONDecoder().decode(Root.self, from: data)
    print(response)
} catch {
    print(error)
}

Note: You can use https://app.quicktype.io to get the models from your JSON instantly. Make the changes as per your requirement and you're good to go.

PGDev
  • 23,751
  • 6
  • 34
  • 88