Jump to content
Sign in to follow this  

C# .NET NewtonSoft JSON Deserializing ListOpenPositionsResponseDTO

Recommended Posts

After a successful GET call to "https://ciapi.cityindex.com/TradingAPI/order/openpositions?TradingAccountId=xxxx" I get the below response 
    "OpenPositions": [
            "OrderId": 776551478,
            "MarketId": 401484347,
            "MarketName": "EUR/USD",
            "Direction": "buy",
            "Quantity": 1000.0000,
            "Price": 1.212940000,
            "TradingAccountId": 403195826,
            "Currency": "USD",
            "Status": 3,
            "StopOrder": null,
            "LimitOrder": null,
            "LastChangedDateTimeUTC": "/Date(1611640457973)/",
            "CreatedDateTimeUTC": "/Date(1611640457972)/",
            "ExecutedDateTimeUTC": "/Date(1611640457972)/",
            "AutoRollover": false,
            "TradeReference": null,
            "ManagedTrades": [],
            "AllocationProfileId": 0,
            "AllocationProfileName": "",
            "AssociatedOrders": {
                "Stop": null,
                "Limit": null
            "FixedInitalMargin": 0.0,
            "PositionMethodId": 1
In my C# .NET App I am failing to convert the JSON text to Object, I am getting exception
                ListOpenPositionsResponseDTO listOpenPositions = JsonConvert.DeserializeObject<ListOpenPositionsResponseDTO>(JSON);
Exception Error Message : 

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ForexAPIGateway.GCAPI_DTO.ApiAssociatedDTO]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'OpenPositions[0].AssociatedOrders.Stop', line 25, position 23.


I have attached the Classes files. Can any one please give me some ideas how to convert to .NET POC Object from JSON? OR Where I need to change? Exception message not much of a help pin-point the exact issue.






Share this post

Link to post

Aaah Dang It! It was my Coding Error, in the "public class ApiOpenPositionDTO" I had 

        public List<ApiAssociatedDTO> AssociatedOrders { get; set; }

but CORRECT Format IS = 

        public ApiAssociatedDTO AssociatedOrders { get; set; }

Now it is DeSerializing just fine.

********* THIS THREAD /POST Can Be deleted as I found My Answer Myself But can be kept for Future Users ******

Share this post

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this