如何将此JsonSAP的抬头+项反序列化为模型

问题描述

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

但是现在我们正在与HEADERS和ITEMS合作。

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

我从Netweaver Gateway获得了JSON返回:

{


"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',"type": "ZLOTO_POC_SRV.WorkPermit_itm"
                },"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')","WoOper": "00000002","Desc": "Second item description","ChangeUser": "TDPO"
            }]
        }
    }
}

解决方法

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

因为我花了一些时间尝试,但模型似乎有些多余;正如我命名的那样,Result。因为一旦我使该类具有所有 possible 属性,它就成为两个级别的首选类;然后反序列化。

这是我的反序列化行:

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,""type"": ""ZLOTO_POC_SRV.WorkPermit_itm""
            },""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)"",""WoOper"": ""00000002"",""Desc"": ""Second item description"",""ChangeUser"": ""TDPO""
          }
        ]
      }
    }
  }
";
}