none
JSON to C# array of objects RRS feed

  • Question

  • I have the following JSON data:

    {"properties":
                [
                    {  "propertyType": "resRef",
                        "values":
                        [  "resource:e40a23ded53bbaeca77cf50ffa63a47f" ]
                    },
                    {  "propertyType": "attrib",
                        "values":
                        [   { "Region": "urn:cms:resource:25d97bf2f0504be3b5bece38fbbf4d17",
                                "Subscription": "urn:cms:resource:202b51d31eaa43eeb9d95686487b6350" }  ]
                    },
                    {  "propertyType": "reseRef",
                        "values": [ "resource:0c968559c7d54cbaaacdcb0dec430156",
                            "resource:4986238930d344a2a6912df93327cdc0",
                            "resource:5a188e216a274ebc84afcefa2eda54bb"      ]
                    }
                ]
    }

    How do I define the [DataContract] classes for it? The values element is either an array of strings or an array of objects.

    Is there a way to parse it based on propertyType? so I can do different types of objects/classes?

    The only way I found is to define it as:

    public class Property { 
    public string propertyType { get; set; } 
    public object[] values { get; set; } 
    } 
    public class RootObject { 
    public Property[] properties { get; set; } 
    }

    I can view the data when the array is of strings, but I cannot see the data when I look for the Region/Subscription.

    Any insight would be appreciated.

    Monday, November 7, 2016 9:23 PM

Answers

  • Hi scanales, 

    >>How do I define the [DataContract] classes for it? The values element is either an array of strings or an array of objects.

    Is there a way to parse it based on propertyType? so I can do different types of objects/classes?

    Sorry, I am not 100% clear about your issue. What do you mean by "based on propertyType"?  Is it from your Json data or others?

       {  "propertyType": "resRef", 
                         "values": 
                         [  "resource:e40a23ded53bbaeca77cf50ffa63a47f" ]
                     }, 

    Here I made a sample based on your Json Data, please have a look

      [DataContract]
        public class Property
        {
            [DataMember]
    
            public string propertyType { get; set; }
            [DataMember]
    
            public value[] values { get; set; }
        }
        [DataContract]
        public class value
        {
    
            [DataMember]
            public string Region { get; set; }
            [DataMember]
            public string Subscription { get; set; }
            [DataMember]
            public string resource { get; set; }
        }
    
        [DataContract]
        public class RootObject
        {
            [DataMember]
            public Property[] properties { get; set; }
        }

    The following part that I make bold, these code can get some value what you want.

     class Program
        {
            static void Main(string[] args)
            {
                string jsonString = GetFileStr("file.txt");
                RootObject obj = JsonDeserialize<RootObject>(jsonString);
                foreach(var a in obj.properties)
                {
                    Console.WriteLine(a.propertyType);
                    foreach(var b in a.values)
                    {
                        Console.WriteLine(b.Region + b.resource);
                    }
                }
                Console.ReadKey();
            }
    
            private static T JsonDeserialize<T>(string jsonString)
            {
                MemoryStream stream1 = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                T obj=(T)ser.ReadObject(stream1);
                return obj;
    
            }
    
            private static string GetFileStr(string path)
            {
                string str = "";
                using (StreamReader sr = new StreamReader(path))
                {
                    str=sr.ReadToEnd();
                }
                return str;
            }
        }

    Hope it helps!

    Best regards,

    Kristin


    Best regards,

    Kristin 


    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.




    Tuesday, November 8, 2016 2:07 PM
    Moderator

All replies

  • Hi scanales, 

    >>How do I define the [DataContract] classes for it? The values element is either an array of strings or an array of objects.

    Is there a way to parse it based on propertyType? so I can do different types of objects/classes?

    Sorry, I am not 100% clear about your issue. What do you mean by "based on propertyType"?  Is it from your Json data or others?

       {  "propertyType": "resRef", 
                         "values": 
                         [  "resource:e40a23ded53bbaeca77cf50ffa63a47f" ]
                     }, 

    Here I made a sample based on your Json Data, please have a look

      [DataContract]
        public class Property
        {
            [DataMember]
    
            public string propertyType { get; set; }
            [DataMember]
    
            public value[] values { get; set; }
        }
        [DataContract]
        public class value
        {
    
            [DataMember]
            public string Region { get; set; }
            [DataMember]
            public string Subscription { get; set; }
            [DataMember]
            public string resource { get; set; }
        }
    
        [DataContract]
        public class RootObject
        {
            [DataMember]
            public Property[] properties { get; set; }
        }

    The following part that I make bold, these code can get some value what you want.

     class Program
        {
            static void Main(string[] args)
            {
                string jsonString = GetFileStr("file.txt");
                RootObject obj = JsonDeserialize<RootObject>(jsonString);
                foreach(var a in obj.properties)
                {
                    Console.WriteLine(a.propertyType);
                    foreach(var b in a.values)
                    {
                        Console.WriteLine(b.Region + b.resource);
                    }
                }
                Console.ReadKey();
            }
    
            private static T JsonDeserialize<T>(string jsonString)
            {
                MemoryStream stream1 = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                T obj=(T)ser.ReadObject(stream1);
                return obj;
    
            }
    
            private static string GetFileStr(string path)
            {
                string str = "";
                using (StreamReader sr = new StreamReader(path))
                {
                    str=sr.ReadToEnd();
                }
                return str;
            }
        }

    Hope it helps!

    Best regards,

    Kristin


    Best regards,

    Kristin 


    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.




    Tuesday, November 8, 2016 2:07 PM
    Moderator
  • Hi scanales,

    you can define a class with [DataContract] and the properties with [DataMember], might be in above case you ate not define the properties with datamember. also try to use the json convertes whic will smoothly convert to C# objects. like newtonsoft converter

    Thanks,

    Vijay

    Tuesday, November 8, 2016 2:57 PM
  • Hi scanales, 

    Please review my first reply. Do you have any updates now?

    If I misunderstood you, please feel free to let me know.

    Best regards,

    Kristin


    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, November 10, 2016 9:33 AM
    Moderator
  • Thank you Kristin for your response, sorry for taking me a while to get back to you.

    The issue that I am having is that there are different types of objects for the values element:

    1. a list of strings (string[])

    2. a list of objects (region and subscription)
    I cannot do:
    [DataContract]   public class value
     
    { [DataMember]   public string Region { get; set; }
          
    [DataMember] public string Subscription { get; set; }
          
    [DataMember] public string resource { get; set; }
       }

    it would have to be either
    [DataContract]   public class value
     
    { [DataMember]   public string Region { get; set; }
         [DataMember] public string Subscription { get; set; }    }

    or


    [DataContract]   public class properties  
    { [DataMember]   public string values{ get; set; }
       }

    Note: there is no resource element (it's all part of a string value...
     "resource:e40a23ded53bbaeca77cf50ffa63a47f"), it is not "resource":"e40a23ded53bbaeca77cf50ffa63a47f".that is why I just did

    [DataMember] public object[] values { get; set; }  so it could be parsed (serialized) correctly.

    I was able to make serialize it as object, but the only question now is how to read the data from that "object" :)

    Thank you


    • Edited by scanales Monday, December 12, 2016 9:35 PM
    Friday, December 9, 2016 5:20 PM
  • Thank you Vijay,
    I was being lazy and I did not include the [DataContract] and [Datamember].

    I tried using newtonsoft, web, and runtime.serialization. Still no luck unless I define the values as an object.

    Thanks

    Friday, December 9, 2016 10:26 PM
  • Hi scanales,

    >>I was able to make serialize it as object, but the only question now is how to read the data from that "object" :)

    Currently, I am afraid there is no other ways except using an Object array. Json is not smart that can recognize these types.

    Best regards,

    Kristin


    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.

    Tuesday, December 13, 2016 9:37 AM
    Moderator