none
How to define a class structure to represent a Json data structure RRS feed

  • Question

  • Hello:

    I have to Serialize some data to Json format, the result is something like this:

    "LimitOrders": "{"Bids":[[1.1, 1.2]], "Asks":[], "Sep":1}~{"Bids":[], "Asks":[],"Sep":1}~{"Bids":[[3.5, 4.1]],"Asks":[[4.5, 5.5]],"Sep":1}",

    The difficult part for me is how to define the class structure for the "LimitOrders".

    The Bids and Asks are nested array of double type data, but some times, Asks have no element, some times, both Bids and Asks have don’t have any element, but an empty array.

    Please advice how I can define a good class structure for the LimitOrder, and I am now using Newtonsoft.Json in Visual Studio 2017 with C# 7.3; for my above "LimitOrders" example, how I can write some code to initialize a JSon data to serialize to have the exact "LimitOrders" output?

    Thanks,

    Friday, September 21, 2018 7:32 PM

Answers

  • Hi zydjohn,

    Please try the code below. Please install Newtonsoft.Json from Manage NuGet Package first.

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Json;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    
    namespace ConsoleApp
    {
        class Serialization_json
        {
            static void Main(string[] args)
            {
                //{"LimitOrders": [ {"Bids":[[1.1, 1.2]], "Asks":[], "S":1},{"Bids":[], "Asks":[],"S":1},{"Bids":[[3.5, 4.1]],"Asks":[[4.5, 5.5]],"S":1} ]}
                List<Limitorder> list = new List<Limitorder>();
                Limitorder limitorder1 = new Limitorder()
                {
                    Bids = new float[][]
                    {
                    new float[2]{1.1F,1.2F}
                    },
                    Asks = new float[][] { },
                    S = 1
                };
                list.Add(limitorder1);
                Limitorder limitorder2 = new Limitorder()
                {
                    Bids = new float[][] { },
                    Asks = new float[][] { },
                    S = 1
                };
                list.Add(limitorder2);
                Limitorder limitorder3 = new Limitorder()
                {
                    Bids = new float[][]
                    {
                    new float[2]{3.5F,4.1F}
                    },
                    Asks = new float[][]
                    {
                     new float[2]{4.5F,5.5F}
                    },
                    S = 1
                };
                list.Add(limitorder3);
                string json = JsonConvert.SerializeObject(list, Newtonsoft.Json.Formatting.None);
                Console.WriteLine(json);
                Console.ReadKey();
            }
        }
    
        public class Rootobject1
        {
            public Limitorder[] LimitOrders { get; set; }
    
        }
    
        public class Limitorder
        {
            public float[][] Bids { get; set; }
            public float[][] Asks { get; set; }
            public int S { get; set; }
        }
    }
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by zydjohn Sunday, October 21, 2018 3:25 PM
    Tuesday, October 9, 2018 7:17 AM
    Moderator

All replies

  • Copy the json into the windows clipboard. In Visual Studio, open a new class. using the following paste into the class.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, September 21, 2018 8:20 PM
    Moderator
  • Hello:

    Thank you very much for your suggestion, it sounds good.   However, due to specific reason, it works for only one entry.  For example, if the Json data is like this: 

    { "LimitOrders": {"Bids":[[1.1, 1.2]], "Asks":[[4.5, 5.5]], "S":1}}

    I can get the class definition like this:

        public class Rootobject
        {
            public Limitorders LimitOrders { get; set; }
        }
    
        public class Limitorders
        {
            public float[][] Bids { get; set; }
            public float[][] Asks { get; set; }
            public int S { get; set; }
        }

    However, if I want to get more entries in the LimitOrder, for example:

    { "LimitOrders": {"Bids":[[1.1, 1.2]], "Asks":[], "S":1}~{"Bids":[], "Asks":[],"S":1} }

    Visual Studio 2017 complains it is not a valid Json format data, the issue is the '~', but I can't figure out how to fix this issue and get a valid Json format data.

    By the way, in the original issue, this part if only a small part of the big data structure (less than 2%), so put everything here is not helpful to solve the issue.  The total big data structure is a valid Json formatted data.

    Friday, September 21, 2018 11:37 PM
  • I think that more entries should look like this:

    { "LimitOrders": [ {"Bids":[[1.1, 1.2]], "Asks":[], "S":1}, {"Bids":[], "Asks":[],"S":1} ] }

    Saturday, September 22, 2018 4:15 AM
  • Hello:

    { "LimitOrders": [ {"Bids":[[1.1, 1.2]], "Asks":[], "S":1},{"Bids":[], "Asks":[],"S":1} ] }

    Is a valid Json format data.  However, when using this data, the generated class looks like:

        public class Rootobject
        {
            public Limitorder[] LimitOrders { get; set; }
        }
    
        public class Limitorder
        {
            public float[][] Bids { get; set; }
            public object[] Asks { get; set; }
            public int S { get; set; }
        }
    

    This class still has some issues:

    1. Under this special case: Bids and Asks are different things: one is float[][], another is object[]; but in reality, both can be float[][] and float[][], or both can be object[] and object[].

    2. To create a class definition, it is OK to replace '~' with ','; however, in reality, I have to use '~' to replace ',', how I can do this with the class definition?

    For example, how I can make a Json object, and serialize it to look like this one?

    {"LimitOrders": [ {"Bids":[[1.1, 1.2]], "Asks":[], "S":1}~{"Bids":[], "Asks":[],"S":1}~{"Bids":[[3.5, 4.1]],"Asks":[[4.5, 5.5]],"S":1} ]}

    Thanks,

    Saturday, September 22, 2018 6:39 AM
  • Hi zydjohn,

    Thank you for posting here.

    The class is generate vis the json data when you use the VS tool. It works for the correct json format data. If the data is not valid json format, you could not generate the classes related to the json data vis VS tool. Also the invalid data is meaningless.

    The json object you provided in your last reply. Based on my test, it is invalid. Please use the valid format.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 24, 2018 5:08 AM
    Moderator
  • Hello:

    Thanks for your reply.  Now I tried the following valid Json format data:

    {"LimitOrders": [ {"Bids":[[1.1, 1.2]], "Asks":[], "S":1},{"Bids":[], "Asks":[],"S":1},{"Bids":[[3.5, 4.1]],"Asks":[[4.5, 5.5]],"S":1} ]}

    In this case, how I can create a Json object and after serialization to see the result like this one?

    Thanks,

    Monday, September 24, 2018 6:00 AM
  • Hi zydjohn,

    Please try the code below. Please install Newtonsoft.Json from Manage NuGet Package first.

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Json;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    
    namespace ConsoleApp
    {
        class Serialization_json
        {
            static void Main(string[] args)
            {
                //{"LimitOrders": [ {"Bids":[[1.1, 1.2]], "Asks":[], "S":1},{"Bids":[], "Asks":[],"S":1},{"Bids":[[3.5, 4.1]],"Asks":[[4.5, 5.5]],"S":1} ]}
                List<Limitorder> list = new List<Limitorder>();
                Limitorder limitorder1 = new Limitorder()
                {
                    Bids = new float[][]
                    {
                    new float[2]{1.1F,1.2F}
                    },
                    Asks = new float[][] { },
                    S = 1
                };
                list.Add(limitorder1);
                Limitorder limitorder2 = new Limitorder()
                {
                    Bids = new float[][] { },
                    Asks = new float[][] { },
                    S = 1
                };
                list.Add(limitorder2);
                Limitorder limitorder3 = new Limitorder()
                {
                    Bids = new float[][]
                    {
                    new float[2]{3.5F,4.1F}
                    },
                    Asks = new float[][]
                    {
                     new float[2]{4.5F,5.5F}
                    },
                    S = 1
                };
                list.Add(limitorder3);
                string json = JsonConvert.SerializeObject(list, Newtonsoft.Json.Formatting.None);
                Console.WriteLine(json);
                Console.ReadKey();
            }
        }
    
        public class Rootobject1
        {
            public Limitorder[] LimitOrders { get; set; }
    
        }
    
        public class Limitorder
        {
            public float[][] Bids { get; set; }
            public float[][] Asks { get; set; }
            public int S { get; set; }
        }
    }
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by zydjohn Sunday, October 21, 2018 3:25 PM
    Tuesday, October 9, 2018 7:17 AM
    Moderator