すべての同様のケースを読んだ後で、以下の解決策を見つけようとしていますが、最後にまだ何かが不足しています。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がnullと表示されます。
EDIT:
私は次の変更に必要な:
string json = reader.ReadToEnd();
var vessel = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine("responseCode : " + vessel.responseCode);
Console.WriteLine("imo : " + vessel.obj[0].imo);
をし、クラスがある:私は知りませんhttp://json2csharp.com/とJSON.NET
必要な特定のタイプにデシリアライズしないのはなぜですか。 – NicoRiff
質問を投稿する前にデバッグを行う必要があります。 jsonは空ですか?そうでない場合は、それを読み取って静的フィールドにして、問題を再現できるようにしてください。 – FINDarkside
[json.net](http://www.newtonsoft.com/json)を参照し、jsonを逆シリアル化してオブジェクトにします。 – BWA