-2

how to deserialize the below Rest response to JSON response my rest response is in the format

{
"new_token":"fdffdsfdsfdsf",
"expires_in":400,
"login_type":"abc"
}

I have a POCO class as

public string NewToken { get; set; }
public string ExpiresIn { get; set; }
public string LoginType { get; set; }

How to store the rest response into the POCO classs

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
madhu
  • 3
  • 5
  • possible duplicate of [Convert JSON String To C# Object](http://stackoverflow.com/questions/4611031/convert-json-string-to-c-sharp-object) – JasonWilczak Jun 11 '15 at 13:02
  • What have you attempted? This should be easy with JSON.NET, there are many existing resources describing how to do this kind of thing. – Andrew Whitaker Jun 11 '15 at 13:26

1 Answers1

0

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;
    }
}
Martin
  • 16,093
  • 1
  • 29
  • 48
  • Thanks martin, but I need in c# – madhu Jun 11 '15 at 13:07
  • I tried adding reference for `System.Web.Extensions` but it fails. my project is targeting `.Net4.5` – madhu Jun 11 '15 at 13:16
  • Is your project targeting `.Net 4.5` or `.Net 4.5 the Client Profile`? This code works perfectly in `.Net 4.5`, but the assembly will be unavailable in the `Client Profile`. – Martin Jun 11 '15 at 13:19
  • project targeting `.Net4.5` martin. Not `.Net4.5 client Profile`. Its really strange, that why its not working – madhu Jun 11 '15 at 13:39
  • When you say that adding the reference to `System.Web.Extensions` fails, what happens when you attempt to add it? – Martin Jun 11 '15 at 13:44
  • finally added it now by reference `System.Web.Script.Serialization`. But after execution the values are null stored in the POCO. The rest response are in format `new_token` but the members in class are `NewToken`. Is that so the values are not deserialized. – madhu Jun 11 '15 at 13:47
  • It works, when I rename the variable as `new_token` instead `NewToken`. But we cant name a POCO class member in lowercase and with underscores – madhu Jun 11 '15 at 13:51
  • @madhu I've updated the answer with a new edit involving `DataContractJsonSerializer`, which offers the ability to map Json property names to .Net properties using the `DataMember` attribute. – Martin Jun 11 '15 at 13:59
  • Thanks martin. I m trying to implement the same – madhu Jun 11 '15 at 14:05
  • martin, sorry for bothering you again dude, I m getting the below error message during deserilization `{"Expecting element 'root' from namespace ''.. Encountered 'None' with name '', namespace ''. "}`. My code is `MemoryStream ms = new MemoryStream {Position = 0}; StreamReader sr = new StreamReader(ms); TokenResponse r = (TokenResponse) ser.ReadObject(ms);` – madhu Jun 11 '15 at 14:50
  • ignore me, I fixed it – madhu Jun 11 '15 at 15:17