serialization to json correctly RRS feed

  • Question

  • I have a set of Odata service on webapi set up.

    One service returns a simple result of:


    note thaat when it gets Jsoned behind the scenes, it includes the data type,  but we don't actually get a named peice of data.
    My end users don't like not having a named piece of data.

    I tried creating a class to store this named data, abd thern serialize it; but now what is returned json says is of type string.

        public class CountClass
            [XmlElement(DataType = "int", ElementName = "TotalCount")]
            public int TotalCount { get; set; }

            public CountClass( int val)
                TotalCount = val;

            var x2 = x.Where(p => p.StateNumber == StateNumber).GroupBy(t1 => t1.OperatorClean).Select(g => g.FirstOrDefault()).Count();
                    var x3 = new CountClass(x2);
                    string strJson = new JavaScriptSerializer().Serialize(x3);
                    return strJson;

    That returnsL  {"@odata.context":"http://localhost:44300/odata/$metadata#Edm.String","value":"{\"TotalCount\":67}"}
    note that I have a named data value now, but the data type is string.

    Thursday, June 29, 2017 6:28 PM

All replies

  • Hello Lee,

    From what I have read the JavaScriptSerializer only supports the ScriptIgnore so you should look into a more sophisticated json serializer like newtonsoft: http://www.newtonsoft.com/jsonschema/help/html/GenerateWithJsonNetAttributes.htm

    Cheers, Jeff

    Thursday, June 29, 2017 9:13 PM
  • Hi Les Stockton,

    Thank you for posting here.

    According to your description, if you want to serialize the data of returnsL, please take the following code for reference.

    If you do not know how to create the class, you could copy the returnsL, and then click Edit in menu, click Paste Special, choose Paste JSON As Classes. It will generate the classes as what you want.

    class Program
            static void Main(string[] args)
                Rootobject ro = new Rootobject
                    odatacontext = "http://localhost:44300/odata/$metadata#Edm.String",
                    value = "{\"TotalCount\":67}"
                // serialize JSON to a string and then write string to a file
                File.WriteAllText("ro.json", JsonConvert.SerializeObject(ro));
                // serialize JSON directly to a file
                using (StreamWriter file = File.CreateText("ro.json"))
                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Serialize(file, ro);
        public class Rootobject
            public string odatacontext { get; set; }
            public string value { get; set; }

    Here is the json file I created.

    Best Regards,


    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.

    Sunday, July 2, 2017 3:05 PM