You just need to deserialise the JSON into your class.
Assuming your class is called Foo
, just use the JavaScriptSerializer
object:
Foo result = new JavaScriptSerializer().Deserialize<Foo>(json);
You will need to add a reference to System.Web.Extensions
in order to access the JavaScriptSerializer
.
EDIT
Following additional clarification regarding the mapping of the raw JSON property names to nice-looking .Net names, and assuming your class is called POCO
, I suggest using the DataContractJsonSerializer
as follows:
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
[DataContract()]
class POCO
{
[DataMember(Name = "new_token")]
public string NewToken { get; set; }
[DataMember(Name = "expires_in")]
public string ExpiresIn { get; set; }
[DataMember(Name = "login_type")]
public string LoginType { get; set; }
}
class SomeClass
{
void DoSomething(string json)
{
MemoryStream reader;
POCO output;
DataContractJsonSerializer serializer;
reader = new MemoryStream(Encoding.Unicode.GetBytes(json));
serializer = new DataContractJsonSerializer(typeof(POCO));
output = serializer.ReadObject(reader) as POCO;
}
}