我試圖在閱讀所有類似案例後找到下面的解決方案,但我仍然錯過了最後的東西。JSON到C#對象無法輸入數據
我的JSON類:
public class Obj
{
public string imo { get; set; }
public string boatName { get; set; }
public string vesselType { get; set; }
public string callSign { get; set; }
public string mmsi { get; set; }
public string gpsTimeStamp { get; set; }
public string lat { get; set; }
public string lon { get; set; }
public string cog { get; set; }
public string sog { get; set; }
public string pollCategory { get; set; }
public string pollMessage { get; set; }
}
public class RootObject
{
public List<Obj> obj { get; set; }
public int objCount { get; set; }
public string responseMessage { get; set; }
public int responseCode { get; set; }
public bool dataTruncated { get; set; }
}
的代碼是:
// After previous statements and functions
WebResponse response = request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string json = reader.ReadToEnd();
Vessel vessel = new Vessel(json);
Console.WriteLine("imo : " + vessel.imo);
Console.WriteLine("boatName : " + vessel.boatName);
// etc etc
public class Vessel
{
public Vessel(string json)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
var jsonObject = serializer.Deserialize<dynamic>(json);
imo = (string)jsonObject["vessel"]["imo"];
boatName = (string)jsonObject["vessel"]["boatName"];
vesselType = (string)jsonObject["vessel"]["vesselType"];
callSign = jsonObject["vessel"]["callSign"];
mmsi = (string)jsonObject["vessel"]["mmsi"];
gpsTimeStamp = (string)jsonObject["vessel"]["gpsTimeStamp"];
lat = (string)jsonObject["vessel"]["lat"];
lon = jsonObject["vessel"]["lon"];
cog = (string)jsonObject["vessel"]["cog"];
sog = (string)jsonObject["vessel"]["sog"];
aisr = (string)jsonObject["vessel"]["aisr"];
pollMessage = jsonObject["vessel"]["pollMessage"];
}
public string imo { get; set; }
public string boatName { get; set; }
public string vesselType { get; set; }
public string callSign { get; set; }
public string mmsi { get; set; }
public string gpsTimeStamp { get; set; }
public string lat { get; set; }
public string lon { get; set; }
public string cog { get; set; }
public string sog { get; set; }
public string aisr { get; set; }
public string pollMessage { get; set; }
}
public class RootObject
{
public List<Vessel> obj { get; set; }
public int objCount { get; set; }
public string responseMessage { get; set; }
public int responseCode { get; set; }
public bool dataTruncated { get; set; }
}
}
但Console.WriteLine命令不會給任何結果。
調試後的obj顯示爲空。
編輯:
我需要以下變化:
string json = reader.ReadToEnd();
var vessel = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine("responseCode : " + vessel.responseCode);
Console.WriteLine("imo : " + vessel.obj[0].imo);
和類是:
public class Obj
{
public string imo { get; set; }
public string boatName { get; set; }
public string vesselType { get; set; }
public string callSign { get; set; }
public string mmsi { get; set; }
public string gpsTimeStamp { get; set; }
public string lat { get; set; }
public string lon { get; set; }
public string cog { get; set; }
public string sog { get; set; }
public string pollCategory { get; set; }
public string pollMessage { get; set; }
}
public class RootObject
{
public List<Obj> obj { get; set; }
public int objCount { get; set; }
public string responseMessage { get; set; }
public int responseCode { get; set; }
public bool dataTruncated { get; set; }
}
感謝http://json2csharp.com/和JSON.NET
爲什麼不只是反序列化到你需要的特定類型? – NicoRiff
在發佈問題之前,您應該進行一些調試。 json是空的嗎?如果不是這樣,只需將其閱讀並將其設置爲靜態字段,以便重現問題。 – FINDarkside
看看[json.net](http://www.newtonsoft.com/json),並將json反序列化爲對象。 – BWA