Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
actually iam new to json..

I have some json string like this:
{"TokenId":"1123", "TokenSecret":"secretcode"}

How to deserialize that json string using c#.net. After that the deserialized string should be displayed in any label or text box what ever it is. and
which namespaces can be used..pls tell me
If any body knows please let me know.
Thanks.
Posted
Updated 12-Jul-11 1:30am
v3

1 solution

If you are using .NET 3.5 or above, you can use the DataContractJsonSerializer class (specifically the ReadObject method). Suppose you had a class that looks like this:
C#
[DataContract]
public class Token
{
  [DataMember(Name="TokenId")]
  public string TokenId { get; set; }
  [DataMember(Name="TokenSecret")]
  public string TokenSecret { get; set; }
}
To deserialize the JSON into this structure, you'd use
C#
Token token = new Token();
var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer();
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json))
{
  token = serializer.ReadObject(ms) as Token;
}
 
Share this answer
 
Comments
komalilella 12-Jul-11 7:31am    
iam not getting datacontract how to achieve tat.
pls let me know which namespaces can be used..
Pete O'Hanlon 12-Jul-11 7:37am    
It's in System.Runtime.Serialization. You need to add a reference to the assembly System.Runtime.Serialization if it's not already present.
komalilella 12-Jul-11 7:41am    
actually wat happend like is it necessary to write [datamember...].
pls let me know.
Sergey Alexandrovich Kryukov 12-Jul-11 13:01pm    
Best option for using Jason, my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900