Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Finance forum / Deserialize Json with ...
Finance forum
Answered

Deserialize Json with multiple contract

Posted on by 240

Hello Guys, 

I'm working in a project that I have a json string, and I want to deserialize it. I did a lot o research, but I couldn't find what I need.
Right now, I can deserialize only simple json, but when it is a nested json I don't know how to proceed. 

I'm using this function to deserialize -> FormJsonSerializer::deserializeObject(classNum(NmbPwbContract), jsonString);

After some research this is how far I got. 

Json:

{
   "ShipmentId": "ShipmentValue",
   "Boxes": [
      {
         "BoxId": "Value",
         "Weight": "Value",
         "Items": [
            {
               "ItemId": "Value",
               "Qty": "1"
            },
            {
               "ItemId": "Value",
               "Qty": "1"
            }
         ]
      },
      {
         "BoxId": "Value",
         "Weight": "Value",
         "Items": {
            "ItemId": "Value",
            "Qty": "1"
         }
      }
   ]
}

RootContract

[DataContractAttribute]
class RootContract
{
    Shipment shipmentId
    List boxes = new List(Types::Class);

    [DataMemberAttribute('ShipmentId')]
    public Shipment parmShipmentId(Shipment _shipmentId = shipmentId)
    {
        shipmentId = _shipmentId;
        return shipmentId;
    }

    [
        DataMemberAttribute('Boxes'),
        AifCollectionTypeAttribute('_boxes', Types::Class, classStr(BoxesContract)),
        AifCollectionTypeAttribute('return', Types::Class, classStr(BoxesContract))
    ]
    public List parmBoxes(List _boxes = boxes)
    {
        boxes = _boxes;
        return boxes;
    }
}

BoxesContract

[DataContractAttribute]
class BoxesContract
{
    str     boxId
    Weight  weight;
    
    [DataMemberAttribute('BoxId')]
    public str parmBoxId(str _boxId = boxId)
    {
        boxId = _boxId;
        return _boxId;
    }

    [DataMemberAttribute('Weight')]
    public real parmWeight(real _weight = weight)
    {
        weight = _weight;
        return _weight;
    }

}

I can retrieve the shipmentid information, but the boxes return empty. 

Any help? Thanks

  • bois0155 Profile Picture
    bois0155 240 on at
    RE: Deserialize Json with multiple contract

    Hello Martin,

    Thank you for the reply, I did what you said, and the values are being populated correctly I can see them on debug mode,

    and I managed to retrieve the values using ListEnumerator like you said.

    Looks like everything is working now thanks.

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 225,588 Super User on at
    RE: Deserialize Json with multiple contract

    You test two many things at once. To know if deserialization works or not, test deserialization only. Simply assign the result of deserializeObject() to a variable, run the code in debugger and check if the variable contains expected data.

    Your second code snippet can't work, because you're not using the enumerator correctly. You would need something like this:

    ListEnumerator enumerator = rootContract.parmBoxes().getEnumerator();
    while (enumerator.moveNext())
    {
    	BoxContract box = enumerator.current();
    	box.parmBoxId();
    }

  • bois0155 Profile Picture
    bois0155 240 on at
    RE: Deserialize Json with multiple contract

    Hello Sergei,

    Thank you for the reply.

    After the suggested changes it is partially working now, after deserialize I did serialize it back just to check the output, and it is getting the information only for the first box, the second one is there, but return empty strings on the values.

    Output json string:

    "{\"Boxes\":[{\"BoxId\":\"Value\",\"Weight\":1.0},{\"BoxId\":\"\",\"Weight\":0.0}],\"ShipmentId\":\"ShipmentValue\"}"

    Also, I tried to retrieve the information like below, but it doesn't work I don't even have the options to select the methods from BoxesContract after .current()

    RootContract.parmBoxes().getEnumerator().current().parmBoxId();

    Error message: 

    An exception of type 'Microsoft.Dynamics.Ax.Xpp.MethodNotFoundException' occurred in aoskernel.dll but was not handled in user code

    Additional information: Error executing code: <empty class> object does not have method 'parmBoxId'

    Any idea about these 2 issues??

  • Verified answer
    Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,083 on at
    RE: Deserialize Json with multiple contract

    Hi bois0155,

    You need to use DataCollectionAttribute for Boxes member in RootContract

    [DataContractAttribute]
    class RootContract
    {
        str shipmentId;
        List boxes = new List(Types::Class);
    
        [DataMemberAttribute('ShipmentId')]
        public str parmShipmentId(str _shipmentId = shipmentId)
        {
            shipmentId = _shipmentId;
            return shipmentId;
        }
    
        [
            DataMemberAttribute('Boxes'),
            DataCollectionAttribute(Types::Class, classStr(BoxesContract))
        ]
        public List parmBoxes(List _boxes = boxes)
        {
            boxes = _boxes;
            return boxes;
        }
    
    }

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,986 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,588 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans