我无法理解如何使用Visual .NET将JSON字符串解析为c#对象。任务很简单,但是我仍然迷路…我得到了这个字符串:
{"single_token":"842269070","username":"example123","version":1.1}
这是我尝试进行消毒的代码:
namespace _SampleProject { public partial class Downloader : Form { public Downloader(string url, bool showTags = false) { InitializeComponent(); WebClient client = new WebClient(); string jsonURL = "http://localhost/jev"; source = client.DownloadString(jsonURL); richTextBox1.Text = source; JavaScriptSerializer parser = new JavaScriptSerializer(); parser.Deserialize<???>(source); }
我不知道在’<’和’>’之间放置什么,从网上阅读的内容中,我必须为其创建一个新的类。另外,如何获得输出?一个例子会有所帮助!
创建一个可以反序列化您的JSON的新类,例如:
public class UserInfo { public string single_token { get; set; } public string username { get; set; } public string version { get; set; } } public partial class Downloader : Form { public Downloader(string url, bool showTags = false) { InitializeComponent(); WebClient client = new WebClient(); string jsonURL = "http://localhost/jev"; source = client.DownloadString(jsonURL); richTextBox1.Text = source; JavaScriptSerializer parser = new JavaScriptSerializer(); var info = parser.Deserialize<UserInfo>(source); // use deserialized info object } }