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

时间:2015-10-14 11:34:49

标签: c# json wpf visual-studio-2015

我有一个项目,我需要使用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 =

2 个答案:

答案 0 :(得分:0)

  

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

使用此包时,可以在一个语句中转换为来自JSON字符串的对象。

示例:

  • 使用Visual Studio创建控制台应用程序JSonSaveLoad
  • 安装NewtonSoft.Json:
  • 在解决方案资源管理器中,右键单击项目的引用,然后选择“管理NUGET包”。
  • 搜索Json.Net并安装

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

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

转换为JSon和从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);
}

答案 1 :(得分:0)

感谢您的回答!我找到了解决方案:

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。