如何将此 Json(Header + 来自 SAP 的项目)反序列化为模型

How do I deserialize this Json (Header + Item from SAP) into a model

我们正在使用 Netweaver Gateway 从 SAP 获取数据。 我有一个 class,我将其与 JsonConvert.DeserializeObject 一起使用,以将 JSON 转换为我的模型。效果很好。

但现在我们正在使用 HEADERS 和 ITEMS。

无法将其转换为(嵌套)模型。

我从 Netweaver Gateway 得到了这个 JSON return:

{


"d": {
        "__metadata": {
            "id": "http://xxx:8000/sap/opu/odata/sap/ZLOTO_POC_SRV/WorkPermit_hdrSet('1')",
            "uri": "http://xxx:8000/sap/opu/odata/sap/ZLOTO_POC_SRV/WorkPermit_hdrSet('1')",
            "type": "ZLOTO_POC_SRV.WorkPermit_hdr"
        },
        "WpNr": "1",
        "WoNr": "123456789",
        "Desc1": "FLOP12",
        "Desc2": "No ieda",
        "Loc1": "",
        "Loc2": "",
        "Execution": "I",
        "ExecDept": "'t smoorkot",
        "ExecComp": "",
        "SupExec": "Big boss",
        "SupExecTel": "+32474895623",
        "VpkInstr": "Mr unknown",
        "VpkInstrTel": "+32474895624",
        "WorkSup": "N/A",
        "WorkSupTel": "+32474895625",
        "Status": "",
        "ValidFrom": null,
        "ValidTo": null,
        "CreateUser": "",
        "ChangeUser": "",
        "WorkPermit_hdr_itm_nav": {
            "results": [{
                "__metadata": {
                    "id": "http://xxx:8000/sap/opu/odata/sap/ZLOTO_POC_SRV/WorkPermit_itmSet(WpNr='1',WoOper='00000001')",
                    "uri": "http://xxx:8000/sap/opu/odata/sap/ZLOTO_POC_SRV/WorkPermit_itmSet(WpNr='1',WoOper='00000001')",
                    "type": "ZLOTO_POC_SRV.WorkPermit_itm"
                },
                "WpNr": "1",
                "WoOper": "00000001",
                "Desc": "First iitem desc",
                "CreateUser": "TDPO",
                "ChangeUser": "TDPO"
            }, {
                "__metadata": {
                    "id": "http://xxx:8000/sap/opu/odata/sap/ZLOTO_POC_SRV/WorkPermit_itmSet(WpNr='1',WoOper='00000002')",
                    "uri": "http://xxx:8000/sap/opu/odata/sap/ZLOTO_POC_SRV/WorkPermit_itmSet(WpNr='1',WoOper='00000002')",
                    "type": "ZLOTO_POC_SRV.WorkPermit_itm"
                },
                "WpNr": "1",
                "WoOper": "00000002",
                "Desc": "Second item description",
                "CreateUser": "TDPO",
                "ChangeUser": "TDPO"
            }]
        }
    }
}

我相信您可能需要查看您的模型,因为它们可能需要更新以处理结构。

因为我尝试了几次,但模型中似乎存在冗余;正如我命名的那样,Result。一旦我使 class 具有所有 可能的 属性,它就变成了两个级别的 go-to class;然后反序列化。

这是我的反序列化行:

var sap = JsonSerializer.Deserialize<SAPData>(GetData());

型号

public class SAPData
{
    public Result d { get; set; }   
}

public class Result
{
    public __metadata __metadata { get; set; }
    public string WpNr { get; set; }
    public string WoNr { get; set; }
    public string Desc1 { get; set; }
    public string Desc2 { get; set; }
    public string Loc1 { get; set; }
    public string Loc2 { get; set; }
    public string Execution { get; set; }
    public string ExecDept { get; set; }
    public string ExecComp { get; set; }
    public string SupExec { get; set; }
    public string SupExecTel { get; set; }
    public string VpkInstr { get; set; }
    public string VpkInstrTel { get; set; }
    public string WorkSup { get; set; }
    public string WorkSupTel { get; set; }
    public string Status { get; set; }
    public string ValidFrom { get; set; }
    public string ValidTo { get; set; }
    public string CreateUser { get; set; }
    public string ChangeUser { get; set; }    
    public string WoOper { get; set; }
    public string Desc { get; set; }

    public WorkPermit_hdr_itm_nav WorkPermit_hdr_itm_nav { get; set; }

}

public class __metadata
{
    public string id { get; set; }
    public string uri { get; set; }
    public string type { get; set; }
}

public class WorkPermit_hdr_itm_nav
{
    public List<Result> results { get; set; }
}

测试JSON

public string GetData()
{
    return @"{

    ""d"": {
      ""__metadata"": {
        ""id"": ""http://xxx:8000/sap/opu/odata/sap/ZLOTO_POC_SRV/WorkPermit_hdrSet(\u00271\u0027)"",
        ""uri"": ""http://xxx:8000/sap/opu/odata/sap/ZLOTO_POC_SRV/WorkPermit_hdrSet(\u00271\u0027)"",
        ""type"": ""ZLOTO_POC_SRV.WorkPermit_hdr""
      },
      ""WpNr"": ""1"",
      ""WoNr"": ""123456789"",
      ""Desc1"": ""FLOP12"",
      ""Desc2"": ""No ieda"",
      ""Loc1"": """",
      ""Loc2"": """",
      ""Execution"": ""I"",
      ""ExecDept"": ""\u0027t smoorkot"",
      ""ExecComp"": """",
      ""SupExec"": ""Big boss"",
      ""SupExecTel"": ""\u002B32474895623"",
      ""VpkInstr"": ""Mr unknown"",
      ""VpkInstrTel"": ""\u002B32474895624"",
      ""WorkSup"": ""N/A"",
      ""WorkSupTel"": ""\u002B32474895625"",
      ""Status"": """",
      ""ValidFrom"": null,
      ""ValidTo"": null,
      ""CreateUser"": """",
      ""ChangeUser"": """",
      ""WorkPermit_hdr_itm_nav"": {
        ""results"": [
          {
            ""__metadata"": {
              ""id"": ""http://xxx:8000/sap/opu/odata/sap/ZLOTO_POC_SRV/WorkPermit_itmSet(WpNr=\u00271\u0027,WoOper=\u002700000001\u0027)"",
              ""uri"": ""http://xxx:8000/sap/opu/odata/sap/ZLOTO_POC_SRV/WorkPermit_itmSet(WpNr=\u00271\u0027,WoOper=\u002700000001\u0027)"",
              ""type"": ""ZLOTO_POC_SRV.WorkPermit_itm""
            },
            ""WpNr"": ""1"",
            ""WoOper"": ""00000001"",
            ""Desc"": ""First iitem desc"",
            ""CreateUser"": ""TDPO"",
            ""ChangeUser"": ""TDPO""
          },
          {
            ""__metadata"": {
              ""id"": ""http://xxx:8000/sap/opu/odata/sap/ZLOTO_POC_SRV/WorkPermit_itmSet(WpNr=\u00271\u0027,WoOper=\u002700000002\u0027)"",
              ""uri"": ""http://xxx:8000/sap/opu/odata/sap/ZLOTO_POC_SRV/WorkPermit_itmSet(WpNr=\u00271\u0027,WoOper=\u002700000002\u0027)"",
              ""type"": ""ZLOTO_POC_SRV.WorkPermit_itm""
            },
            ""WpNr"": ""1"",
            ""WoOper"": ""00000002"",
            ""Desc"": ""Second item description"",
            ""CreateUser"": ""TDPO"",
            ""ChangeUser"": ""TDPO""
          }
        ]
      }
    }
  }
";
}