[英]Deserialization of Json without name fields
我需要反序列化以下 Json,根據 Jsonlint.com 是有效的,但我以前沒有遇到過或者找不到類似 Json 的例子以及如何處理它?
[1,"Bellegrove / Sherwood ","76705","486","Bexleyheath Ctr",1354565507000]
我目前的系統是這樣的:
數據類:
[DataContract]
public class TFLCollection
{ [DataMember(Name = "arrivals")]
public IEnumerable<TFLB> TFLB { get; set; }
}
[DataContract]
public class TFLB
{
[DataMember]
public string routeName { get; set; }
[DataMember]
public string destination { get; set; }
[DataMember]
public string estimatedWait { get; set; }
}
解串器:
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(TFLCollection));
using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(result)))
{ var buses = (TFLCollection)serializer.ReadObject(stream);
foreach (var bus in buses.TFLBuses)
{
StopFeed _item = new StopFeed();
_item.Route = bus.routeName;
_item.Direction = bus.destination;
_item.Time = bus.estimatedWait;
listBox1.Items.Add(_item);
我現有的反序列化器使用完整的 Json 流並對其進行迭代,但在我的新 Json 中,我需要反序列化,它只有 1 個項目,因此我不需要對其進行迭代。
那么是否可以使用與我目前類似的方法反序列化我的 Json 示例?
我會說你試圖把事情復雜化。 您擁有的是一個完美形成的 json 字符串數組。 如果我是你,我會先將它反序列化為 .net 數組,然后編寫一個“映射器”函數來復制值:
public TFLB BusRouteMapper(string[] input)
{
return new TFLB {
Route = input[x],
Direction = input[y],
};
}
等等。 當然,這假設您知道 json 的順序,但是如果您首先嘗試這樣做,那么您必須這樣做!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.