获取json数据并将其放入变量中

本文关键字:变量 json 数据 获取 | 更新日期: 2023-09-27 18:17:34

我有一个项目,我需要得到数据json,然后把它放入一个数据网格。

我是这样得到数据的:

using (WebClient webClient = new WebClient())
{
     string email = loginDialog.email;
     string password = loginDialog.password;
     WebClient http = new WebClient();
     http.Headers.Add("Content-Type", "application/json");
     http.Headers.Add("OSLC-Core-Version", "2.0");
     //Windows login: Email + Password
     http.Credentials = new NetworkCredential(email, password);
     using(Stream stream = http.OpenRead(stringURL))
     {
        XmlDocument doc = new XmlDocument();
        doc.Load(stream);
        string json = JsonConvert.SerializeXmlNode(doc);
        //Console.WriteLine(json);
     }
}

这里我得到json数据…现在如何将数据(只是优先级)放在变量中?

// priority = 
// BTQStatus = 
// implementationDate =

获取json数据并将其放入变量中

如果你知道你要加载的类的类型,考虑使用NUGET包NewtonSoft.Json。

当使用这个包时,对象和JSON字符串之间的转换在一个语句中完成。

的例子:

  • 使用Visual Studio创建控制台应用程序JSonSaveLoad
  • 安装NewtonSoft。Json:
  • 在解决方案资源管理器中,右键单击项目的引用并选择Manage NUGET packages。
  • 查找Json。Net并安装

用一些简单的属性创建一个类:

    public class Person
    {
        public string Name { get; set; }
        public DateTime BirthDay { get; set; }
        public DateTime? DeathDay { get; set; }
    }

与JSon之间的转换操作如下:

static void Main(string[] args)
{
    // fill a list with Persons and Pupils
    var personalHeroes = new List<Person>()
    {
        new Person()
        {
            Name = "Charley Chaplin",
            BirthDay = new DateTime(1890, 8, 4),
            DeathDay = new DateTime(1977, 12, 25),
        },
        new Person()
        {
            Name = "Winston Churchill",
            BirthDay = new DateTime(1885, 4, 18),
            DeathDay = new DateTime(1965, 01,24),
        },
        new Person()
        {
            Name = "Pope Franciscus",
            BirthDay = new DateTime(1936, 12, 17)
            // not Death yet!
        },
    };
    // JSON serialize to file, also write result to screen
    var tmpFileName = System.IO.Path.GetTempFileName();
    using (TextWriter writer = new StreamWriter(tmpFileName))
    {
        string jsonTxt = JsonConvert.SerializeObject(personalHeroes , Formatting.Indented);
        Console.WriteLine(jsonTxt);
        writer.Write(jsonTxt);
    }
    // deserialize
    using (TextReader reader = new StreamReader(tmpFileName))
    {
        var jsonTxt = reader.ReadToEnd();
        var deserializedHeroes = JsonConvert.DeserializeObject<List<Person>>(jsonTxt);
    }
    File.Delete(tmpFileName);
}

谢谢你的回答!我找到了一个解决方案:

using (WebClient webClient = new WebClient())
{
    string email = loginDialog.email;
    string password = loginDialog.password;

    webClient.Headers.Add("Content-Type", "application/json");
    webClient.Headers.Add("OSLC-Core-Version", "2.0");
    //Windows login: Email + Password
    webClient.Credentials = new NetworkCredential(email, password);
    using (Stream stream = webClient.OpenRead(URLstring)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(stream);
        string json = JsonConvert.SerializeXmlNode(doc);
        JObject searchResult = JObject.Parse(json);
        if (searchResult.GetValue("oslc_cm:totalCount").Value<int>() == 1) {
        using (Stream BTQStream = webClient.OpenRead(searchResult.SelectToken("oslc_cm:results").First.SelectToken("rdf:resource").Value<String>()))
        {
           XmlDocument BTQdoc = new XmlDocument();
           BTQdoc.Load(BTQStream);
           string BTQJson = JsonConvert.SerializeXmlNode(BTQdoc);
           JObject BTQEntry = JObject.Parse(BTQJson);
           priority = BTQEntry.SelectToken("Severity.oslc_cm:label").Value<String>();
           BTQStatus = BTQEntry.GetValue("State").Value<String>();
           implementationDate = Convert.ToDateTime(BTQEntry.GetValue("actualdate_impl").Value<String>());
        }
        }
    }

}

现在它从URLstring获取数据并将其放入dataGrid。