Я пытаюсь найти решение ниже, прочитав все подобные случаи, но я все еще не хватает чего-то в самом конце.JSON to 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 после отладки кажется нулевым.
EDIT:
Я понадобились следующие изменения:
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