请帮我解决以下问题... [英] pls help me for below question...

查看:107
本文介绍了请帮我解决以下问题...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上是json的新手..

我有一些像这样的json字符串:
{"TokenId":"1123","TokenSecret":"secretcode"}

如何使用c#.net反序列化json字符串.之后,反序列化的字符串应显示在任何标签或文本框中.和
.pls告诉我
如果有人知道,请告诉我.

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.

推荐答案

如果使用的是.NET 3.5或更高版本,则可以使用DataContractJsonSerializer类(特别是ReadObject方法).假设您有一个看起来像这样的类:
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:
[DataContract]
public class Token
{
  [DataMember(Name="TokenId")]
  public string TokenId { get; set; }
  [DataMember(Name="TokenSecret")]
  public string TokenSecret { get; set; }
}

要将JSON反序列化为这种结构,您可以使用

To deserialize the JSON into this structure, you''d use

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


这篇关于请帮我解决以下问题...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆