2015-10-14 51 views
0

我有一个项目,我需要使用json获取数据,然后将其放入数据网格中。获取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 = 
+0

'var json'包含XML或JSON? – aloisdg

+0

您可以反序列化'JSON' /'XML'。 – Sybren

+0

@aloisdg var json包含XML。 – Purger86

回答

0

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

使用此包将对象转换为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完成如下:

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); 
} 
+0

我正在使用Newtonsoft.json; 你从哪里得到(serializedP)? – Purger86

+0

哎呀,我的错。我纠正了它。反序列化是从读取字符串到您指定类型的对象:JSonConvert。DeserializeObject (string)将返回类型T的对象,如果该字符串包含它的Json表示。唯一的缺点:你必须有权访问T的类型定义 –

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。

相关问题