我想從一個JSON鏈接獲取一些信息並以友好的方式將其顯示給用戶。我什麼都試過了,似乎無法讓我的周圍this.Basically的頭,我想要顯示的用戶C#JSON到每個項目
ID = someValue中
GUID = someValue中
名稱= someValue中 等
我有我的JSON後,我去了json2csharp並得到了我的課如下圖所示:
public class Computer
{
public string ID { get; set; }
public string GUID { get; set; }
public string name { get; set; }
public string type { get; set; }
public string entity { get; set; }
public string serial { get; set; }
public string uuid { get; set; }
public string inventorynumber { get; set; }
public string status { get; set; }
public string site { get; set; }
public string location { get; set; }
public string manufacturer { get; set; }
public string model { get; set; }
public string owner { get; set; }
public string lastuser { get; set; }
public string domain { get; set; }
public string os { get; set; }
public string servicepack { get; set; }
public string osversion { get; set; }
}
public class Data
{
public List<Computer> computers { get; set; }
}
public class RootObject
{
public string state { get; set; }
public Data data { get; set; }
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.xxx/?xxx=xxx&format=json ");
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
var rawJson = reader.ReadToEnd();
RootObject rootResult = JsonConvert.DeserializeObject<RootObject>(rawJson);
Console.Write();
Console.ReadKey();
}
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
Console.WriteLine(errorText);
}
throw;
}
與此代碼,如果我做
Console.WriteLine(rootResult.state);
我得到我的「成功」價值。但我找不到實際獲取「計算機」下的數據的方法 任何人都可以告訴我我做錯了什麼嗎?
我也嘗試過類似的問題,但它們看起來都不一樣,因爲我的JSON格式有點不同。
在此先感謝。
執行此操作時'rootResult.data'中有什麼? – oerkelens
我得到JsonDemo.Program +數據(JsonDemo是我的解決方案的名稱) – Besiktas
您可以發佈您試圖反序列化的JSON嗎? –