我无法理解如何解析JSON字符串转换成C#与Visual .NET对象。这个任务是很容易的,但我还是输了...... 我得到这个字符串:
{single_token:842269070,用户名:example123,版本:1.1}
这是code,我尝试desterilize:
命名空间_SampleProject
{
公共部分类下载:形态
{
公共下载(字符串URL,布尔showTags = FALSE)
{
的InitializeComponent();
Web客户端的客户端=新的Web客户端();
字符串jsonURL =HTTP://本地主机/乙脑;
来源= client.DownloadString(jsonURL);
richTextBox1.Text =来源;
JavaScriptSerializer分析器=新JavaScriptSerializer();
parser.Deserialize< ???>(来源);
}
我不知道之间的放什么'<和>,并从我在线阅读,我必须创建一个新的类,它..?另外,我怎么输出? 一个例子将是有益的 解决方案
创建您的JSON可以反序列化,例如一个新的类:
公共类的UserInfo
{
公共字符串single_token {获得;组; }
公共字符串的用户名{获得;组; }
公共字符串版本{获得;组; }
}
公共部分类下载:形态
{
公共下载(字符串URL,布尔showTags = FALSE)
{
的InitializeComponent();
Web客户端的客户端=新的Web客户端();
字符串jsonURL =HTTP://本地主机/乙脑;
来源= client.DownloadString(jsonURL);
richTextBox1.Text =来源;
JavaScriptSerializer分析器=新JavaScriptSerializer();
VAR信息= parser.Deserialize<的UserInfo>(来源);
//使用反序列化信息对象
}
}
I'm having trouble understanding how to parse JSON string into c# objects with Visual .NET. The task is very easy, but I'm still lost... I get this string:
{"single_token":"842269070","username":"example123","version":1.1}
And this is the code where I try to desterilize:
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);
}
I don't know what to put between the '<' and '>', and from what I've read online I have to create a new class for it..? Also, how do I get the output? An example would be helpful!
解决方案Create a new class that your JSON can be deserialized into such as:
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
}
}