简体   繁体   English

将JSON对象反序列化为的类混淆

[英]Class confusion for deserializing JSON object to

I'm using json2csharp.com and pasted my Json raw data in it to create class for deserializing object to, but it's generated many classes ( RootObject and other called " __invalid_type__" ) and it's now confusing me... as I got many " __invalid_type__ ". 我正在使用json2csharp.com并将我的Json原始数据粘贴到其中,以创建用于反序列化对象的类,但是它生成了许多类( RootObject和其他名为__invalid_type__的类 ),现在它使我感到困惑,因为我得到了很多“ __invalid_type__ “。

My question is now : Do I also need to use __invalid_type__ class in my C# source code? 现在的问题是:我还需要在C#源代码中使用__invalid_type__类吗?

Below is the raw-json 下面是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"} 
      
     

Below is the generated class from json2csharp.com 以下是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; }
}

the correct class structure for your JSON structure can be also represented as 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 remains the same rootObject保持不变

It is probably better to use QuickType ( https://app.quicktype.io/#l=cs ) because it will generate Dictionary properties. 使用QuickType( https://app.quicktype.io/#l=cs )可能更好,因为它将生成Dictionary属性。 Dictionary is needed because the source JSON has numeric keys such as "1" which can't be translated to C# class property names. 需要Dictionary ,因为源JSON具有不能转换为C#类属性名称的数字键,例如"1"

I cleaned the JSON to this because the tool kept complaining about backslashes: 我为此清理了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":"...."}

Partial output (*): 部分输出(*):

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; }
}

(*) : Generated using the option [Just Types]. (*):使用选项[Just Types]生成。 You can get more complete code using one of the other output options. 您可以使用其他输出选项之一来获取更完整的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM