可以在类型脚本中加载对象的复杂JSON类型

时间:2019-03-28 01:57:47

标签: json typescript

我有一个复杂的JSON类型,我想在Typescript中加载到我的模型中。问题是我从第三方收到此JSON,他们将数据作为对象而不是数组发送。有什么方法可以将数据加载到模型中吗?

{
   "members":{
      "8392883213":{
         "requestingCoverageIndicator":true,
         "demographic":{
            "ssn":"999888888",
            "birthDate":"1980-01-01",
            "name":{
               "firstName":"Susan",
               "middleName":"Carrie",
               "lastName":"Griffith",
               "suffix":"Jr."
            }
         }
      },
      "8392883222":{
         "requestingCoverageIndicator":true,
         "demographic":{
            "ssn":"999888888",
            "birthDate":"1980-01-01",
            "name":{
               "firstName":"Susan",
               "middleName":"Carrie",
               "lastName":"Griffith",
               "suffix":"Jr."
            }
         }
      }
   }
}

2 个答案:

答案 0 :(得分:1)

是您要找的东西吗?

const bla = {
   "members":{
      "8392883213":{
         "requestingCoverageIndicator":true,
         "demographic":{
            "ssn":"999888888",
            "birthDate":"1980-01-01",
            "name":{
               "firstName":"Susan",
               "middleName":"Carrie",
               "lastName":"Griffith",
               "suffix":"Jr."
            }
         }
      },
      "8392883222":{
         "requestingCoverageIndicator":true,
         "demographic":{
            "ssn":"999888888",
            "birthDate":"1980-01-01",
            "name":{
               "firstName":"Susan",
               "middleName":"Carrie",
               "lastName":"Griffith",
               "suffix":"Jr."
            }
         }
      }
   }
};
console.log(Object.keys(bla.members).map(ele => bla.members[ele]));

答案 1 :(得分:0)

尝试

interface Members {
    [key: string]: Indicator;
}

interface Indicator {
    requestingCoverageIndicator: boolean;
    demographic: Demographic;
}

interface Demographic {
    ssn: string;
    birthDate: string;
    name: Name;
}

interface Name {
    firstName: string;
    middleName: string;
    lastName: string;
    suffix: string;
}