10

我无法理解如何使用 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);
        }

我不知道在“<”和“>”之间放什么,从我在网上阅读的内容来看,我必须为它创建一个新类..?另外,我如何获得输出?一个例子会很有帮助!

4

7 回答 7

9

创建一个可以将您的 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
    }
}
于 2012-05-03T13:49:48.940 回答
7

如果您使用的是 .NET 4 - 使用动态数据类型。

http://msdn.microsoft.com/en-us/library/dd264736.aspx

string json = "{ single_token:'842269070', username: 'example123', version:1.1}";

     JavaScriptSerializer jss = new JavaScriptSerializer();

     dynamic obj = jss.Deserialize<dynamic>(json);

     Response.Write(obj["single_token"]);
     Response.Write(obj["username"]);
     Response.Write(obj["version"]); 
于 2012-05-03T13:47:30.860 回答
2

是的,您需要一个具有与您的 JSON 匹配的属性的新类。

MyNewClass result = parser.Deserialize<MyNewClass>(source);
于 2012-05-03T13:43:06.703 回答
2

通常的方法是创建一个类(或一组类,用于更复杂的 JSON 字符串)来描述您要反序列化的对象并将其用作通用参数。

另一种选择是将 JSON 反序列化为Dictionary<string, object>

parser.Deserialize<Dictionary<string, object>>(source);

这样,您可以访问数据,但除非您必须这样做,否则我不建议您这样做。

于 2012-05-03T13:54:08.013 回答
1

您需要一个与您获得的 JSON 匹配的类,它将返回该类的一个新对象,其中填充了值。

于 2012-05-03T13:44:07.133 回答
0

以下是代码..

ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

        request = WebRequest.Create("https://myipaddress/api/admin/configuration/v1/conference/1/");

        request.Credentials = new NetworkCredential("admin", "admin123");
        // Create POST data and convert it to a byte array.
        request.Method = "GET";          

                // Set the ContentType property of the WebRequest.
        request.ContentType = "application/json; charset=utf-8";          


        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        JavaScriptSerializer js = new JavaScriptSerializer();
        var obj = js.Deserialize<dynamic>(responseFromServer);
        Label1.Text = obj["name"];
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();
于 2016-09-12T12:22:50.843 回答
0
dynamic data = JObject.Parse(jsString);
var value= data["value"];
于 2018-05-10T23:27:13.800 回答