簡體   English   中英

將JSON對象反序列化為的類混淆

[英]Class confusion for deserializing JSON object to

我正在使用json2csharp.com並將我的Json原始數據粘貼到其中,以創建用於反序列化對象的類,但是它生成了許多類( RootObject和其他名為__invalid_type__的類 ),現在它使我感到困惑,因為我得到了很多“ __invalid_type__ “。

現在的問題是:我還需要在C#源代碼中使用__invalid_type__類嗎?

下面是raw-json

 {"Id":31, "Title":"Japan Toyoda", "SubTitle":"Japenese, WR14 3HE", "Description":"\
    
    
    
      ", "InvestStrategy":"\ 
     
       ", "InvestType":"NorthBounds", "TargetReturn":"50.0000", "InvestTerm":12,"MinimalPrice":"1.00","SharePrice":1, "TotalShares":597826, "AvailableShares":186670, "BoughtShares":411156, "InformationMemorandumUrl":"https:\\/\\/www.google.jp\\/properties\\/propertyInformationFile\\/hokidoRoad", "MainImage":"https:\\/\\/www.mysite.com\\/properties_images\\/image0001.png", "Images":["https:\\/\\/www.mysite.com\\/properties_images\\/image078901.png","https:\\/\\/www.mysite.com\\/properties_images\\/6a5940077eec9cc283.JPG","https:\\/\\/www.mysite.com\\/properties_images\\/4f9036fa91ede.jpeg"], "Plan":["https:\\/\\/www.mysite.com\\/properties_images\\/4f9036694fa91ede.jpeg"], "ListAttributes":{"1":{"name":"Second Charge Security","icon":"checking-square","value":""},"2":{"name":"","icon":"checking-square-o","value":""},"3":{"name":"Second Board","icon":"checking-square-o","value":""},"4":{"name":"Water","icon":"fa-check-square-o","value":""}}, "Latitude":"130.10599000","Longitude":"-78.210610000","Url":"https:\\/\\/www.mysite.com\\/ref\\/bb88f8054731878b209c\\/youtoo"} 
      
     

以下是json2csharp.com生成的類

public class __invalid_type__1
{
    public string name { get; set; }
    public string icon { get; set; }
    public string value { get; set; }
}

public class __invalid_type__2
{
    public string name { get; set; }
    public string icon { get; set; }
    public string value { get; set; }
}

public class __invalid_type__3
{
    public string name { get; set; }
    public string icon { get; set; }
    public string value { get; set; }
}

public class __invalid_type__4
{
    public string name { get; set; }
    public string icon { get; set; }
    public string value { get; set; }
}

public class ListAttributes
{
    public __invalid_type__1 __invalid_name__1 { get; set; }
    public __invalid_type__2 __invalid_name__2 { get; set; }
    public __invalid_type__3 __invalid_name__3 { get; set; }
    public __invalid_type__4 __invalid_name__4 { get; set; }
}

public class RootObject
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string SubTitle { get; set; }
    public string Description { get; set; }
    public string InvestStrategy { get; set; }
    public string InvestType { get; set; }
    public string TargetReturn { get; set; }
    public int InvestTerm { get; set; }
    public string MinimalInvest { get; set; }
    public int SharePrice { get; set; }
    public int TotalShares { get; set; }
    public int AvailableShares { get; set; }
    public int BoughtShares { get; set; }
    public string InformationMemorandumUrl { get; set; }
    public string MainImage { get; set; }
    public List<string> Images { get; set; }
    public List<string> FloorPlan { get; set; }
    public ListAttributes ListAttributes { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string Url { get; set; }
}

JSON結構的正確類結構也可以表示為

public class Listattributes
{
    public dataObject _1 { get; set; }
    public dataObject _2 { get; set; }
    public dataObject _3 { get; set; }
    public dataObject _4 { get; set; }
}

public class dataObject
{
    public string name { get; set; }
    public string icon { get; set; }
    public string value { get; set; }
}

rootObject保持不變

使用QuickType( https://app.quicktype.io/#l=cs )可能更好,因為它將生成Dictionary屬性。 需要Dictionary ,因為源JSON具有不能轉換為C#類屬性名稱的數字鍵,例如"1"

我為此清理了JSON,因為該工具一直抱怨反斜杠:

{"Id":31,
"Title":"....",
"SubTitle":"....",
"Description":"....",
"InvestStrategy":"....",
"InvestType":"....",
"TargetReturn":"50.0000",
"InvestTerm":12,
"MinimalPrice":"1.00",
"SharePrice":1,
"TotalShares":597826,
"AvailableShares":186670,
"BoughtShares":411156,
"InformationMemorandumUrl":"....",
"MainImage":"....",
"Images":["....","....","...."],
"Plan":["...."],
"ListAttributes":{"1":{"name":"....","icon":"....","value":""},"2":{"name":"","icon":"....","value":""},"3":{"name":"....","icon":"....","value":""},"4":{"name":"Water","icon":"....","value":""}},
"Latitude":"130.10599000",
"Longitude":"-78.210610000",
"Url":"...."}

部分輸出(*):

public partial class JsonItem
{
    public long Id { get; set; }

    // Other props...

    public string[] Images { get; set; }
    public string[] Plan { get; set; }
    public Dictionary<string, ListAttribute> ListAttributes { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string Url { get; set; }
}

public partial class ListAttribute
{
    public string Name { get; set; }
    public string Icon { get; set; }
    public string Value { get; set; }
}

(*):使用選項[Just Types]生成。 您可以使用其他輸出選項之一來獲取更完整的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM