Working with JSON in C# .NET 3.5

后端 未结 3 1350
执念已碎
执念已碎 2021-01-07 04:37

I have found some helper method that allow me to convert an object to JSONM and JSON to an object. Now I am reading in a json file that looks something like this:



        
相关标签:
3条回答
  • 2021-01-07 05:20

    I have changed the above 'From' Method to this one, in order to get an array of objects:

    public static T Deserialize<T>(string json)
        {
            T obj;
            using (MemoryStream ms = new MemoryStream(Encoding.Default.GetBytes(json)))
            {
                System.Runtime.Serialization.Json.DataContractJsonSerializer deserializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
                obj = (T)desirializer.ReadObject(ms);
            }
    
            return obj;
        }
    

    Then, in case you have an array of objects :

    Person[] personArray = JsonHelper.Deserialize<Person[]>(input);
    

    or if you have one object :

    Person person = JsonHelper.Deserialize<Person>(input);
    
    0 讨论(0)
  • 2021-01-07 05:21

    Download Json.NET. That handles JSON better than anything else I've seen for .NET. I think the Json serializer will do what you're asking.

    Also, take a look at the related questions over there on the right. I do believe you'll find the answer there.

    0 讨论(0)
  • 2021-01-07 05:30

    You need to use DataContractJsonSerializer which is in the System.Runtime.Serialization.Json namespace. Mark your class with the [DataContract] attribute, collection classes with the [CollectionDataContract] attribute and the properties with the [DataMember] attribute.

    [CollectionDataContract]
    public class People : List<Person>
    {
    
    }
    
    [DataContract]
    public class Person
    {
         public Person() { }
    
         [DataMember]
         public int Id{ get; set; }
    
         [DataMember]
         public string Name { get; set; }
    }
    

    Here is a helper class to serialize (To) and deserialize (From)

    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Json;
    
    public class jsonHelper
    {
        public static string To<T>(T obj)
        {
            string retVal = null;
            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
             serializer.WriteObject(ms, obj);
             retVal = Encoding.Default.GetString(ms.ToArray());
            }
    
            return retVal;
        }
    
        public static T From<T>(string json)
        {
            T obj = Activator.CreateInstance<T>();
            using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            {
             System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
             obj = (T)serializer.ReadObject(ms);
            }
    
            return obj;
        }
    }
    

    So take your json above and send it to the From method in the jsonHelper class above

    People peeps = jsonHelper.From<People>(input);
    
    0 讨论(0)
提交回复
热议问题