none
How to deserialize JSON string in .NET 3.5? RRS feed

  • Question

  • I have a client who wants to send a JSON string containing arrays with one or more complex items to a .NET application running version 3.5 with a desire to not depend upon third party libraries.

    Here's an example of a passed in JSON string:

    {
        "users": [
            {
                "mail_cid": "14193de4-c772-4ee2-8aa3-8d1b06cbf6f2",
                "display_name": "Richard Milhouse",
                "org_info": null,
                "luser_info": "{\"info\": {\"psids\": [\"S-1-16-4096\", \"S-1-1-0\", \"S-1-5-114\", \"S-1-5-21-2744474471-2561516044-1743272080-1002\", \"S-1-5-32-544\", \"S-1-5-32-562\", \"S-1-5-32-559\", \"S-1-5-32-545\", \"S-1-5-4\", \"S-1-2-1\", \"S-1-5-11\", \"S-1-5-15\", \"S-1-11-96-3623454863-58364-18864-2661722203-1597581903-1437548037-3970083271-2547338903-499754375-3403269841\", \"S-1-5-113\", \"S-1-5-5-0-637817\", \"S-1-2-0\", \"S-1-5-64-36\"], \"sid\": \"S-1-5-21-2744474471-2561516044-1743272080-1001\"}, \"platform\": \"win32\"}",
                "device": null,
                "user_id": "milhouse@msn.com",
                "password": "O%r{hOwP>n!_3}nTBl9R)]gD{|+=CPponr_",
                "mua_info": {
                    "imap_password": "O%r{hOwP>n!_3}nTBl9R)]gD{|+=CPponr_",
                    "imap_port": 4000,
                    "smtp_password": "O%r{hOwP>n!_3}nTBl9R)]gD{|+=CPponr_",
                    "imap_username": "milhouse@msn.com",
                    "smtp_port": 4001,
                    "imap_addr": "127.0.0.1",
                    "smtp_username": "milhouse@msn.com",
                    "smtp_addr": "127.0.0.1"
                }
            },
            {
                "mail_cid": "249b4a68-c41e-4c4a-8811-3dce4cf19692",
                "display_name": "Richard Milhouse",
                "org_info": null,
                "luser_info": "{\"info\": {\"psids\": [\"S-1-16-4096\", \"S-1-1-0\", \"S-1-5-114\", \"S-1-5-21-2744474471-2561516044-1743272080-1002\", \"S-1-5-32-544\", \"S-1-5-32-562\", \"S-1-5-32-559\", \"S-1-5-32-545\", \"S-1-5-4\", \"S-1-2-1\", \"S-1-5-11\", \"S-1-5-15\", \"S-1-11-96-3623454863-58364-18864-2661722203-1597581903-1437548037-3970083271-2547338903-499754375-3403269841\", \"S-1-5-113\", \"S-1-5-5-0-131886475\", \"S-1-2-0\", \"S-1-5-64-36\"], \"sid\": \"S-1-5-21-2744474471-2561516044-1743272080-1001\"}, \"platform\": \"win32\"}",
                "device": null,
                "user_id": "RichardMilhouse@gmail.com",
                "password": "Opow8(|.aH;&IY|mof$48&FJm8T9Tr,g,6.",
                "mua_info": {
                    "imap_password": "Opow8(|.aH;&IY|mof$48&FJm8T9Tr,g,6.",
                    "imap_port": 4000,
                    "smtp_password": "Opow8(|.aH;&IY|mof$48&FJm8T9Tr,g,6.",
                    "imap_username": "RichardMilhouse@gmail.com",
                    "smtp_port": 4001,
                    "imap_addr": "127.0.0.1",
                    "smtp_username": "RichardMilhouse@gmail.com",
                    "smtp_addr": "127.0.0.1"
                }
            }
        ]
    }
    So, keeping in mind that the client really wants to avoid third party libraries and the .NET 3.4 limitation, what sort of JSON deserialization technology should I use to keep my client happy?

    Richard Lewis Haggard


    Wednesday, June 27, 2018 1:15 AM

Answers

  • Hi Richard.Haggard,

    You could also use DataContractJsonSerializer to deserialize JSON string. I test the code on .net framework 3.5. Works well. Please try the steps below.

    1. Add the class based on json file.

    Copy your json string. Click Edit> Paste Special> Paste JSON As Classes. After that, you could get the classes you want.

    2. Add reference of System.ServiceModel.Web.

    3. Here is the code.

     class Program
        {
            static void Main(string[] args)
            {
                DeserializeJson();
                Console.ReadKey();
            }
            //DataContractJsonSerializer
            public static void DeserializeJson()
            {
                var json = File.ReadAllText("DeserializeJson.json");
                using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
                {
                    // Deserialization from JSON  
                    DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(Rootobject));
                    Rootobject bsObj2 = (Rootobject)deserializer.ReadObject(ms);
                    foreach (var item in bsObj2.users)
                    {
                        Console.WriteLine(item.luser_info);
                        Console.WriteLine(item.mua_info);
                    }
                }
            }
    
        }
    
        public class Rootobject
        {
            public User[] users { get; set; }
        }
    
        public class User
        {
            public string mail_cid { get; set; }
            public string display_name { get; set; }
            public object org_info { get; set; }
            public string luser_info { get; set; }
            public object device { get; set; }
            public string user_id { get; set; }
            public string password { get; set; }
            public Mua_Info mua_info { get; set; }
        }
    
        public class Mua_Info
        {
            public string imap_password { get; set; }
            public int imap_port { get; set; }
            public string smtp_password { get; set; }
            public string imap_username { get; set; }
            public int smtp_port { get; set; }
            public string imap_addr { get; set; }
            public string smtp_username { get; set; }
            public string smtp_addr { get; set; }
        }

    4. Here is the result of 'luser_info' and 'mua_info' string.

    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.

    Thursday, June 28, 2018 7:04 AM
    Moderator

All replies

  • You can use:

    • System.Web.Script.Serialization.JavaScriptSerializer  https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.90).aspx
    • System.Runtime.Serialization.Json https://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer(v=vs.90).aspx

    But, most are now using the Newtonsoft library. You might show the client a new MVC project created in Visual Studio and show that Microsoft is using the Newtonsoft library and not the various .NET Framework JSON libraries.



    Mike Smith TechTrainingNotes.blogspot.com
    Books: SharePoint 2007 2010 Customization for the Site Owner, SharePoint 2010 Security for the Site Owner

    Wednesday, June 27, 2018 1:53 AM
  • As I understand it, in the supplied example the input file contains an array of two items. Most of the items are pretty easy to deal with but what about 'luser_info' and 'mua_info'? These look like nested objects. Is it good enough to specify these as included class objects within the outside 'user' object like is normally done in a .NET object deserialization?


    Richard Lewis Haggard

    Thursday, June 28, 2018 12:53 AM
  • luser_info is just a string, and will need to be JSON parsed as a separate operation.

    I created a simplified version to test the nested mua_info object, and it worked fine.

    using System;
    
    namespace ConsoleApplication4
    {
        class Program
        {
            static void Main(string[] args)
            {
                string jsontext = @"
                {
                'display_name': 'Richard Milhouse',
                'user_id': 'milhouse@msn.com',
                'luser_info': '{\'info\': {\'psids\': [\'S-1-16-4096\', \'S-1-1-0\', \'S-1-5-114\', \'S-1-5-21-2744474471-2561516044-1743272080-1002\', \'S-1-5-32-544\', \'S-1-5-32-562\', \'S-1-5-32-559\', \'S-1-5-32-545\', \'S-1-5-4\', \'S-1-2-1\', \'S-1-5-11\', \'S-1-5-15\', \'S-1-11-96-3623454863-58364-18864-2661722203-1597581903-1437548037-3970083271-2547338903-499754375-3403269841\', \'S-1-5-113\', \'S-1-5-5-0-637817\', \'S-1-2-0\', \'S-1-5-64-36\'], \'sid\': \'S-1-5-21-2744474471-2561516044-1743272080-1001\'}, \'platform\': \'win32\'}',
                'mua_info': {
                    'imap_port': 4000,
                    'imap_username': 'milhouse@msn.com'
                  }
                }
                ";
    
                System.Web.Script.Serialization.JavaScriptSerializer js = 
                    new System.Web.Script.Serialization.JavaScriptSerializer();
                user u = js.Deserialize<user>(jsontext);
    
                Console.WriteLine(u.luser_info);
                Console.WriteLine(u.mua_info.imap_username);
                Console.ReadLine();
            }
        }
    
        class user
        {
            public string display_name { get; set; }
            public string user_id { get; set; }
            public string luser_info { get; set; }
            public mua_infotype mua_info { get; set; }
    
        }
    
        class mua_infotype
        {
            public string imap_port { get; set; }
            public string imap_username { get; set; }
        }
    }
    


    Mike Smith TechTrainingNotes.blogspot.com
    Books: SharePoint 2007 2010 Customization for the Site Owner, SharePoint 2010 Security for the Site Owner

    Thursday, June 28, 2018 4:34 AM
  • Hi Richard.Haggard,

    You could also use DataContractJsonSerializer to deserialize JSON string. I test the code on .net framework 3.5. Works well. Please try the steps below.

    1. Add the class based on json file.

    Copy your json string. Click Edit> Paste Special> Paste JSON As Classes. After that, you could get the classes you want.

    2. Add reference of System.ServiceModel.Web.

    3. Here is the code.

     class Program
        {
            static void Main(string[] args)
            {
                DeserializeJson();
                Console.ReadKey();
            }
            //DataContractJsonSerializer
            public static void DeserializeJson()
            {
                var json = File.ReadAllText("DeserializeJson.json");
                using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
                {
                    // Deserialization from JSON  
                    DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(Rootobject));
                    Rootobject bsObj2 = (Rootobject)deserializer.ReadObject(ms);
                    foreach (var item in bsObj2.users)
                    {
                        Console.WriteLine(item.luser_info);
                        Console.WriteLine(item.mua_info);
                    }
                }
            }
    
        }
    
        public class Rootobject
        {
            public User[] users { get; set; }
        }
    
        public class User
        {
            public string mail_cid { get; set; }
            public string display_name { get; set; }
            public object org_info { get; set; }
            public string luser_info { get; set; }
            public object device { get; set; }
            public string user_id { get; set; }
            public string password { get; set; }
            public Mua_Info mua_info { get; set; }
        }
    
        public class Mua_Info
        {
            public string imap_password { get; set; }
            public int imap_port { get; set; }
            public string smtp_password { get; set; }
            public string imap_username { get; set; }
            public int smtp_port { get; set; }
            public string imap_addr { get; set; }
            public string smtp_username { get; set; }
            public string smtp_addr { get; set; }
        }

    4. Here is the result of 'luser_info' and 'mua_info' string.

    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.

    Thursday, June 28, 2018 7:04 AM
    Moderator
  • Your very well expressed answer did it for me. Thanks.

    Richard Lewis Haggard

    Monday, July 2, 2018 12:45 AM