Working with JSON in C# .NET 3.5

33,828

Solution 1

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);

Solution 2

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.

Share:
33,828
ryanzec
Author by

ryanzec

A software engineer for severals years in web development with PHP/MySQL now focusing on front-end development with HTML, CSS, and JavaScript (AngularJS). Also and inspiring game developer using the Unity game engine.

Updated on September 03, 2020

Comments

  • ryanzec
    ryanzec about 3 years

    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:

    /************************************************************************/
    /* Coments Here *********************************************************/
    /************************************************************************/
    //more comments  
    [{
      "Id": 1,
      "Name": "HP Up"
    },
    {
      "Id": 2,
      "Name": "Regeneration"
    }]
    

    Now while I can convert JSON the represents 1 object, I would I go about converting this in multiple objects with C# .NET 3.5?

  • ryanzec
    ryanzec almost 13 years
    That is what I already have but that won't work for multiple objects in the JSON like the example one given. I want to be able to read in a JSON object with multiple objects and put them into a list I just don't know how to break up the JSON string from the file into multiple objects.
  • Shayne Boyer
    Shayne Boyer almost 13 years
    The example here will deserialize the JSON into a list of objects of type Person. Then you can iterate the collection and inspect each object individually.