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

571 views Asked by At

We are using Netweaver Gateway to get data from SAP. I have a class which I use with JsonConvert.DeserializeObject<ODataObject> to get the JSON converted to my model. Which works fine.

But now we are working with HEADERS and ITEMS.

Can't get it converted to a (nested) model.

I got this JSON return from Netweaver Gateway:

{


"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"
            }]
        }
    }
}
1

There are 1 answers

0
ΩmegaMan On BEST ANSWER

I believe you might have to look at your models, for they might need to be updated to handle the structures.

For it took me a few tries but there seems to be a redundancy in the model; as I named it, Result. For once I made that class have all possible properties, it became the go-to class for both levels; it then deserialized.

Here is my deserialization line:

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

Models

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; }
}

Test 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""
          }
        ]
      }
    }
  }
";
}