解决  面对包括Json类中的Dictionary方法的问题

帕沙

知名会员
已加入
2020年4月29日
留言内容
64
编程经验
1-3
我有转换为JSON类的JSON字符串,现在我想添加Dictionary方法插入我的JSON类,它将从JSON类变量返回某些值

为此,我面临一个问题,包括Json类c#中的Dictionary方法

有人可以帮我在哪里添加该方法?

My JSON Class Model:
namespace JsonModel
{
    using Newtonsoft.Json;
    using System.Collections.Generic;

    public partial class StringResourcesX
    {
        [JsonProperty("StringResources")]
        public StringResources StringResources { get; set; }
    }

    public partial class StringResources
    {
        
        [JsonProperty("Culture")]
        public Culture Culture { get; set; }

        public Culture_ Culture_ { get; set; }
        public FullName FullName { get; set; }

        public Dictionary<Culture_, FullName> CulturalNames { get; set; }
    }
    
    

    public partial class Culture
    {
        [JsonProperty("Cultures")]
        public Cultures Cultures { get; set; }

        [JsonProperty("CulturalName")]
        public CulturalName CulturalName { get; set; }
    }

    public partial class CulturalName
    {
        [JsonProperty("AE")]
        public AE AE { get; set; }

        [JsonProperty("GB")]
        public GB GB { get; set; }

        
    }

    public partial class AE
    {
        [JsonProperty("AE_Name")]
        public string AE_Name { get; set; }

        [JsonProperty("AE_LastName")]
        public string AE_LastName { get; set; }
    }

    
    public partial class GB
    {
        [JsonProperty("GB_Name")]
        public string GB_Name { get; set; }

        [JsonProperty("GB_LastName")]
        public string GB_LastName { get; set; }
    }

    public partial class Cultures
    {
        [JsonProperty("Culture_UK")]
        public string Culture_UK { get; set; }

        [JsonProperty("Culture_US")]
        public string Culture_US { get; set; }

        [JsonProperty("Culture_CA")]
        public string Culture_CA { get; set; }

        [JsonProperty("Culture_DE")]
        public string Culture_DE { get; set; }

        [JsonProperty("Culture_JP")]
        public string Culture_JP { get; set; }
    }

    public enum Culture_
    {
        GB,
        US,
        IT,
        FR,
        CA,
        DE,
        SE,
        NL,
        NZ,
        AU,
        AE,
        CN,
        JP,
        IE,
        IN,
        ES
    }

    public class FullName
    {
        public string First;
        public string Last;
        public FullName(string first, string last)
        {
            this.First = first;
            this.Last = last;
        }
    }
}


我想在类模型中包括的方法:
public static Dictionary<Culture_, FullName> CulturalNames = new Dictionary<Culture_, FullName>
        {
            {Culture_.GB, new FullName(Culture.CulturalName.GB.GB_Name, Culture.CulturalName.GB.GB_LastName)},{Culture_.AE, new FullName(Culture.CulturalName.AE.AE_Name, Culture.CulturalName.AE.AE_LastName)}
        };

但我得到编译时错误为"字段初始值设定项不能引用非静态字段",因此我无法理解如何以及在哪里应包括可以使用的方法
 

羊皮

退休程序员
工作人员
已加入
2018年9月5日
留言内容
1,926
地点
英国
编程经验
10+
如果字段不是静态的。您遵循什么标准来访问非静态字段?

有两个答案,这取决于您是想重用该字段还是在每次需要重用它时覆盖该字段值。你需要哪一个?

编辑错字
 

帕沙

知名会员
已加入
2020年4月29日
留言内容
64
编程经验
1-3
如果字段不是静态的。您遵循什么标准来访问静态字段?

有两个答案,这取决于您是想重用该字段还是在每次需要重用它时覆盖该字段值。你需要哪一个?
所以基本上我想要的是,我想使用名字和姓氏的组合作为文化名称,以便从我们从JSON字符串获得的数据中,在JSON类中希望将其用于dictionalry方法中,可以在不同的类中使用它们
 

羊皮

退休程序员
工作人员
已加入
2018年9月5日
留言内容
1,926
地点
英国
编程经验
10+
我可以看到您要执行的操作。但是您没有回答我问的问题。
 

羊皮

退休程序员
工作人员
已加入
2018年9月5日
留言内容
1,926
地点
英国
编程经验
10+
重新阅读我的第一个回复。抱歉,我的意思是非静态的。哈哈

基本上,您需要问自己是否正在使用的属性应该由实例或静态修饰符调用。
 

帕沙

知名会员
已加入
2020年4月29日
留言内容
64
编程经验
1-3
重新阅读我的第一个回复。抱歉,我的意思是非静态的。哈哈

基本上,您需要问自己是否正在使用的属性应该由实例或静态修饰符调用。
我需要静态修饰符,因此可以在其他类中使用它们,如下所示


C#:
if (StringResources.CulturalNames.ContainsKey(cultureCode))
            {
                payerDetails.UserDetails.Name = StringResources.CulturalNames[cultureCode].First;
                payerDetails.UserDetails.LastName = StringResources.CulturalNames[cultureCode].Last;
            }
 

羊皮

退休程序员
工作人员
已加入
2018年9月5日
留言内容
1,926
地点
英国
编程经验
10+
将您的类设为静态或删除static修饰符。
 

帕沙

知名会员
已加入
2020年4月29日
留言内容
64
编程经验
1-3
我不想在JSON类模型中使用static修饰符,如果我不使用它,则会出现编译错误,因此我无法找到一种处理方式

如果我不使用静态,基本上会收到以下错误
附注:我正在分享图片,因为那样就很容易理解错误

1597242153373.png


1597242170080.png
 

帕沙

知名会员
已加入
2020年4月29日
留言内容
64
编程经验
1-3
我可以找到如下所述的解决方案

C#:
namespace ContradoWebsiteAutoFramework.JsonModel
{
    using Framework.Config;
    using Framework.Helpers;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    using Culture_ = Framework.Constants.Culture;

    public class StringResourcesTestData
    {
        public const string jsonFilePath = "JsonData\\Constants\\StringResources.json";
        public static StringResourcesX StringResources { get; set; }

        private StringResourcesTestData()
        {

        }

        public static StringResourcesX Get()
        {
            if(StringResources == null)
                StringResources = JsonHelper
                    .GetTestParams<StringResourcesJson>(ConfigProvider.BuildPath(jsonFilePath))
                    .StringResources;

            return StringResources;
        }
    }

    public partial class StringResourcesJson
    {
        [JsonProperty("StringResources")]
        public StringResourcesX StringResources { get; set; }
    }

    public partial class StringResourcesX
    {
    
        [JsonProperty("Culture")]
        public Culture Culture { get; set; }

    }

  
    public partial class Culture
    {
        [JsonProperty("Cultures")]
        public Cultures Cultures { get; set; }

        [JsonProperty("CulturalName")]
        public CulturalName CulturalName { get; set; }
      
    }

    public partial class CulturalName
    {   
        public class FullName
        {
            public string First;
            public string Last;
            public FullName(string first, string last)
            {
                this.First = first;
                this.Last = last;
            }
        }
        public Dictionary<Culture_, FullName> CulturalNames;

        public FullName GetName(Culture_ culture_)
        {
            if (CulturalNames == null)
            {
                CulturalNames = new Dictionary<Culture_, FullName>
                {
                    {Culture_.GB, new FullName (GB.GB_Name, GB.GB_LastName)},{Culture_.US, new FullName(US.US_Name ,US.US_LastName)},
                    {Culture_.IT, new FullName(IT.IT_Name, IT.IT_LastName)},{Culture_.ES, new FullName(ES.ES_Name, ES.ES_LastName)},
                    {Culture_.FR, new FullName(FR.FR_Name, FR.FR_LastName)},{Culture_.AE, new FullName(AE.AE_Name, AE.AE_LastName)},
                    {Culture_.DE, new FullName(DE.DE_Name, DE.DE_LastName)},{Culture_.NL, new FullName(NL.NL_Name, NL.NL_LastName)},
                    {Culture_.JP, new FullName(JP.JP_Name, JP.JP_LastName)},{Culture_.SE, new FullName(SE.SE_Name, SE.SE_LastName)},
                    {Culture_.IE, new FullName(IE.IE_Name, IE.IE_LastName)},{Culture_.AU, new FullName(AU.AU_Name, AU.AU_LastName)},
                    {Culture_.CA, new FullName(CA.CA_Name, CA.CA_LastName)},{Culture_.NZ, new FullName(NZ.NZ_Name, NZ.NZ_LastName)},
                    {Culture_.IN, new FullName(IN.IN_Name, IN.IN_LastName)},{Culture_.CN, new FullName(CN.CN_Name, CN.CN_LastName)}
                };
            }

            return CulturalNames[culture_];
        }


        [JsonProperty("AE")]
        public AE AE { get; set; }

        [JsonProperty("GB")]
        public GB GB { get; set; }

    }

    public partial class AE
    {
        [JsonProperty("AE_Name")]
        public string AE_Name { get; set; }

        [JsonProperty("AE_LastName")]
        public string AE_LastName { get; set; }
    }

  

    public partial class GB
    {
        [JsonProperty("GB_Name")]
        public string GB_Name { get; set; }

        [JsonProperty("GB_LastName")]
        public string GB_LastName { get; set; }
    }

}

But one question, is there a way where I can 为字典字段建模? @跳伞 , @谢平 -您能帮忙吗?
 
Last edited:

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,533
地点
弗吉尼亚州切萨皮克
编程经验
10+
你到底是什么意思"为字典字段建模"?对我来说,听起来像您只需要执行 词典<TKey, TValue>.

我仍在尝试了解您上面的代码。尚不清楚为什么需要从JSON加载某些内容,但同时又有一些硬编码的字典值。
 

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,533
地点
弗吉尼亚州切萨皮克
编程经验
10+
据我了解,您的JSON如下所示。尽管看起来很紧凑,但是针对以下情况编写代码不是很友好:
JSON:
{
    "StringResources" : {
        "Culture" : {
            "Cultures" : {
                "Culture_AE" : "United Arab Emirates",
                "Culture_DE" : "Deustch",
                "Culture_FR" : "French",
                "Culture_JP" : "Japanese",
                "Culture_US" : "United States"
            },
            "CulturalName" : {
                "AE" : {
                    "AE_Name" : "Omar",
                    "AE_LastName" : "Ali"
                },
                "DE" : {
                    "DE_Name" : "Hans",
                    "DE_LastName" : "Muller"
                },
                "FR" : {
                    "FR_Name" : "Louis",
                    "FR_LastName" : "Bisset"
                },
                "JP" : {
                    "JP_Name" : "Hiro",
                    "JP_LastName" : "Tanaka"
                },
                "US" : {
                    "US_Name" : "Aaron",
                    "US_LastName" : "Smith"
                }
            }
        }
    }
}

我建议将JSON重组为更常规的代码,并且更容易针对以下代码编写代码:
JSON:
{
    "StringResources" : {
        "Cultures" : [
            {
                "Culture" : {
                    "Code" : "AE",
                    "Name" : "United Arab Emirates",
                    "CulturalName" : {
                        "Name" : "Omar",
                        "LastName" : "Ali"
                    }   
                }   
            },
            {
                "Culture" : {
                    "Code" : "DE",
                    "Name" : "Deustch",
                    "CulturalName" : {
                        "Name" : "Hans",
                        "LastName" : "Muller"
                    }   
                }   
            },
            {
                "Culture" : {
                    "Code" : "FR",
                    "Name" : "French",
                    "CulturalName" : {
                        "Name" : "Louis",
                        "LastName" : "Bisset"
                    }   
                }   
            },
            {
                "Culture" : {
                    "Code" : "JP",
                    "Name" : "Japanese",
                    "CulturalName" : {
                        "Name" : "Hiro",
                        "LastName" : "Tanaka"
                    }   
                }   
            },
            {
                "Culture" : {
                    "Code" : "US",
                    "Name" : "United States",
                    "CulturalName" : {
                        "Name" : "Aaron",
                        "LastName" : "Smith"
                    }   
                }   
            }
        ]
    }
}
 

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,533
地点
弗吉尼亚州切萨皮克
编程经验
10+
I couldn't sleep so here's some code to muddle through. All of them access a dictionary using a 2 letter country code to get some kind of Culture object that has the details. It should be no surprise that if you store the data in JSON as a dictionary, then it should read back in as a dictionary.

C#:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;

class FullName
{
    public string Name { get; set; }
    public string LastName { get; set; }

    public override string ToString() => $"{Name} {LastName}";
}

class LoadArray
{
    class Culture
    {
        public string Code { get; set; }
        public string Name { get; set; }
        public FullName CulturalName { get; set; }
    }

    class StringResources
    {
        public IList<Culture> Cultures { get; set; }

        [JsonIgnore]
        public IDictionary<string, Culture> Culture => Cultures.ToDictionary(c => c.Code);
    }

    public void Run()
    {
        var jsonString = File.ReadAllText(@"StringResources1.json");
        var jobject = JObject.Parse(jsonString);
        var stringResources = jobject["StringResources"].ToObject<StringResources>();

        Console.WriteLine(stringResources.Culture["JP"].CulturalName);
    }
}

class LoadArray2
{
    class Culture
    {
        public string Name { get; set; }
        public FullName CulturalName { get; set; }
    }

    class StringResources
    {
        public IDictionary<string, Culture> Cultures { get; set; }
    }

    public void Run()
    {
        var jsonString = File.ReadAllText(@"StringResources1.json");
        var jobject = JObject.Parse(jsonString);
        var stringResources = new StringResources()
        {
            Cultures = jobject["StringResources"]
                              ["Cultures"]
                              .ToDictionary(t => t["Code"].ToString(),
                                            t => t.ToObject<Culture>())
        };

        Console.WriteLine(stringResources.Cultures["JP"].CulturalName);
    }
}

class LoadDictionary
{
    class Culture
    {
        public string Name { get; set; }
        public FullName CulturalName { get; set; }
    }

    class StringResources
    {
        public IDictionary<string, Culture> Cultures { get; set; }
    }

    public void Run()
    {
        var jsonString = File.ReadAllText(@"StringResources2.json");
        var jobject = JObject.Parse(jsonString);
        var stringResources = jobject["StringResources"].ToObject<StringResources>();

        Console.WriteLine(stringResources.Cultures["JP"].CulturalName);
    }
}

class Program
{
    static void Main()
    {
        new LoadArray().Run();
        new LoadArray2().Run();
        new LoadDictionary().Run();
    }
}

StringResources1.json:
{
  "StringResources": {
    "Cultures": [
      {
        "Code": "AE",
        "Name": "United Arab Emirates",
        "CulturalName": {
          "Name": "Omar",
          "LastName": "Ali"
        }
      },
      {
        "Code": "DE",
        "Name": "Deustch",
        "CulturalName": {
          "Name": "Hans",
          "LastName": "Muller"
        }
      },
      {
        "Code": "FR",
        "Name": "French",
        "CulturalName": {
          "Name": "Louis",
          "LastName": "Bisset"
        }
      },
      {
        "Code": "JP",
        "Name": "Japanese",
        "CulturalName": {
          "Name": "Hiro",
          "LastName": "Tanaka"
        }
      },
      {
        "Code": "US",
        "Name": "United States",
        "CulturalName": {
          "Name": "Aaron",
          "LastName": "Smith"
        }
      }
    ]
  }
}

StringResources2.json:
{
  "StringResources": {
    "Cultures": {
      "AE": {
        "Name": "United Arab Emirates",
        "CulturalName": {
          "Name": "Omar",
          "LastName": "Ali"
        }
      },
      "DE": {
        "Name": "Deustch",
        "CulturalName": {
          "Name": "Hans",
          "LastName": "Muller"
        }
      },
      "FR": {
        "Name": "French",
        "CulturalName": {
          "Name": "Louis",
          "LastName": "Bisset"
        }
      },
      "JP": {
        "Name": "Japanese",
        "CulturalName": {
          "Name": "Hiro",
          "LastName": "Tanaka"
        }
      },
      "US": {
        "Name": "United States",
        "CulturalName": {
          "Name": "Aaron",
          "LastName": "Smith"
        }
      }
    }
  }
}
 

帕沙

知名会员
已加入
2020年4月29日
留言内容
64
编程经验
1-3
是的
你到底是什么意思"为字典字段建模"?对我来说,听起来像您只需要执行 词典<TKey, TValue>.

我仍在尝试了解您上面的代码。尚不清楚为什么需要从JSON加载某些内容,但同时又有一些硬编码的字典值。
基本上我想序列化字典也许像 这些 ,而且我的代码有点复杂,那里的数据很少"Name" and "Lastname"根据文化,然后我想将它们添加到字典中

现在反序列化之后,要访问字典值,我必须使用单例,而不是那样。如果我可以在json中序列化该字典字段,它本身就是自序列,因此可以直接访问

但是我不知道该怎么做


JSON Class:
namespace Framework.JsonModel
{
    using Framework.Config;
    using Framework.Helpers;
    using Newtonsoft.Json;
    using System.Collections.Generic;
    using System.ComponentModel;

    public class StringResourcesTestData
    {
        public const string jsonFilePath = "JsonData\\Constants\\StringResources.json";
        public static StringResourcesX StringResources { get; set; }

        private StringResourcesTestData()
        {

        }

        public static StringResourcesX Get()
        {
            if(StringResources == null)
                StringResources = JsonHelper
                    .GetTestParams<StringResourcesJson>(ConfigProvider.BuildPath(jsonFilePath))
                    .StringResources;

            return StringResources;
        }
    }

    public partial class StringResourcesJson
    {
        [JsonProperty("StringResources")]
        public StringResourcesX StringResources { get; set; }
    }

    public partial class StringResourcesX
    {
        [JsonProperty("Culture")]
        public Culture Culture { get; set; }

     
    }

   

    public partial class Culture
    {
        [JsonProperty("Cultures")]
        public Cultures Cultures { get; set; }

        [JsonProperty("CulturalName")]
        public CulturalName CulturalName { get; set; }
       
    }

    public partial class CulturalName
    {
        public enum CultureEnum
        {
            GB,
            US,
            IT,
            FR,
            CA,
            DE,
            SE,
            NL,
            NZ,
            AU,
            AE,
            CN,
            JP,
            IE,
            IN,
            ES
        }
        public class FullName
        {
            public string First;
            public string Last;
            public FullName(string first, string last)
            {
                this.First = first;
                this.Last = last;
            }
        }
        public Dictionary<CultureEnum, FullName> CulturalNames;

        public FullName GetName(CultureEnum culture_)
        {
            if (CulturalNames == null)
            {
                CulturalNames = new Dictionary<CultureEnum, FullName>
                {
                    {CultureEnum.GB, new FullName (GB.Name, GB.LastName)},{CultureEnum.US, new FullName(US.Name ,US.LastName)},
                    {CultureEnum.IT, new FullName(IT.Name, IT.LastName)},{CultureEnum.ES, new FullName(ES.Name, ES.LastName)},
                    {CultureEnum.FR, new FullName(FR.Name, FR.LastName)},{CultureEnum.AE, new FullName(AE.Name, AE.LastName)},
                    {CultureEnum.DE, new FullName(DE.Name, DE.LastName)},{CultureEnum.NL, new FullName(NL.Name, NL.LastName)},
                    {CultureEnum.JP, new FullName(JP.Name, JP.LastName)},{CultureEnum.SE, new FullName(SE.Name, SE.LastName)},
                    {CultureEnum.IE, new FullName(IE.Name, IE.LastName)},{CultureEnum.AU, new FullName(AU.Name, AU.LastName)},
                    {CultureEnum.CA, new FullName(CA.Name, CA.LastName)},{CultureEnum.NZ, new FullName(NZ.Name, NZ.LastName)},
                    {CultureEnum.IN, new FullName(IN.Name, IN.LastName)},{CultureEnum.CN, new FullName(CN.Name, CN.LastName)}
                };
            }

            return CulturalNames[culture_];
        }


        [JsonProperty("AE")]
        public CultureNameDetails AE { get; set; }

        [JsonProperty("GB")]
        public CultureNameDetails GB { get; set; }

        [JsonProperty("US")]
        public CultureNameDetails US { get; set; }

        [JsonProperty("CN")]
        public CultureNameDetails CN { get; set; }

        [JsonProperty("IT")]
        public CultureNameDetails IT { get; set; }

        [JsonProperty("ES")]
        public CultureNameDetails ES { get; set; }

        [JsonProperty("FR")]
        public CultureNameDetails FR { get; set; }

        [JsonProperty("DE")]
        public CultureNameDetails DE { get; set; }

        [JsonProperty("NL")]
        public CultureNameDetails NL { get; set; }

        [JsonProperty("SE")]
        public CultureNameDetails SE { get; set; }

        [JsonProperty("IE")]
        public CultureNameDetails IE { get; set; }

        [JsonProperty("AU")]
        public CultureNameDetails AU { get; set; }

        [JsonProperty("CA")]
        public CultureNameDetails CA { get; set; }

        [JsonProperty("NZ")]
        public CultureNameDetails NZ { get; set; }

        [JsonProperty("IN")]
        public CultureNameDetails IN { get; set; }

        [JsonProperty("JP")]
        public CultureNameDetails JP { get; set; }
    }

    public partial class CultureNameDetails
    {
        [JsonProperty("Name")]
        public string Name { get; set; }

        [JsonProperty("LastName")]
        public string LastName { get; set; }
    }

    public partial class Cultures
    {
        [JsonProperty("Culture_UK")]
        public string Culture_UK { get; set; }

        [JsonProperty("Culture_US")]
        public string Culture_US { get; set; }

        [JsonProperty("Culture_CA")]
        public string Culture_CA { get; set; }

        [JsonProperty("Culture_DE")]
        public string Culture_DE { get; set; }

        [JsonProperty("Culture_JP")]
        public string Culture_JP { get; set; }
    }
 
}


JSON Data:
{
  "StringResources": {
    "Culture": {
      "Cultures": {
        "Culture_UK": "en-GB",
        "Culture_US": "en-US",
        "Culture_CA": "en-CA",
        "Culture_DE": "de-DE",
        "Culture_JP": "ja-JP"
      },
      "CulturalName": {
        "AE": {
          "Name": "Abu'l-Fadl",
          "LastName": "Isa"
        },
        "GB": {
          "Name": "Jamie",
          "LastName": "Lanister"
        },
        "US": {
          "Name": "Tyrion",
          "LastName": "Lanister"
        },
        "CN": {
          "Name": "陶",
          "LastName": "致远"
        },
        "IT": {
          "Name": "Clelia",
          "LastName": "Calabrese"
        },
        "ES": {
          "Name": "Sebastián",
          "LastName": "Hinojosa Argüello"
        },
        "FR": {
          "Name": "L'Heureux",
          "LastName": "Boulé"
        },
        "DE": {
          "Name": "Jörg",
          "LastName": "Gärtner"
        },
        "NL": {
          "Name": "Selma",
          "LastName": "Løkkeberg"
        },
        "SE": {
          "Name": "Ekström",
          "LastName": "Åberg"
        },
        "IE": {
          "Name": "Bailey",
          "LastName": "Quinn"
        },
        "AU": {
          "Name": "Lola",
          "LastName": "Patel"
        },
        "CA": {
          "Name": "Max",
          "LastName": "Godfrey"
        },
        "NZ": {
          "Name": "Edward",
          "LastName": "Bevan"
        },
        "IN": {
          "Name": "Anshu",
          "LastName": "Chahal"
        },
        "JP": {
          "Name": "ナルト",
          "LastName": "うずまき"
        }
      }
    }
  }
}

这就是我当前访问的方式


C#:
var StringResourceData = StringResourcesTestData.Get();
            var t1 = StringResourcesTestData.Get().Culture.CulturalName.GetName(Constants.Culture.GB);
            var t2 = StringResourcesTestData.Get().Culture.CulturalName.GetName(Constants.Culture.AE);
 
Last edited:

帕沙

知名会员
已加入
2020年4月29日
留言内容
64
编程经验
1-3
Yes

基本上我想序列化字典也许像 这些 ,而且我的代码有点复杂,那里的数据很少"Name" and "Lastname"根据文化,然后我想将它们添加到字典中

现在反序列化之后,要访问字典值,我必须使用单例,而不是那样。如果我可以在json中序列化该字典字段,它本身就是自序列,因此可以直接访问

但是我不知道该怎么做


JSON Class:
namespace Framework.JsonModel
{
    using Framework.Config;
    using Framework.Helpers;
    using Newtonsoft.Json;
    using System.Collections.Generic;
    using System.ComponentModel;

    public class StringResourcesTestData
    {
        public const string jsonFilePath = "JsonData\\Constants\\StringResources.json";
        public static StringResourcesX StringResources { get; set; }

        private StringResourcesTestData()
        {

        }

        public static StringResourcesX Get()
        {
            if(StringResources == null)
                StringResources = JsonHelper
                    .GetTestParams<StringResourcesJson>(ConfigProvider.BuildPath(jsonFilePath))
                    .StringResources;

            return StringResources;
        }
    }

    public partial class StringResourcesJson
    {
        [JsonProperty("StringResources")]
        public StringResourcesX StringResources { get; set; }
    }

    public partial class StringResourcesX
    {
        [JsonProperty("Culture")]
        public Culture Culture { get; set; }

    
    }

  

    public partial class Culture
    {
        [JsonProperty("Cultures")]
        public Cultures Cultures { get; set; }

        [JsonProperty("CulturalName")]
        public CulturalName CulturalName { get; set; }
      
    }

    public partial class CulturalName
    {
        public enum CultureEnum
        {
            GB,
            US,
            IT,
            FR,
            CA,
            DE,
            SE,
            NL,
            NZ,
            AU,
            AE,
            CN,
            JP,
            IE,
            IN,
            ES
        }
        public class FullName
        {
            public string First;
            public string Last;
            public FullName(string first, string last)
            {
                this.First = first;
                this.Last = last;
            }
        }
        public Dictionary<CultureEnum, FullName> CulturalNames;

        public FullName GetName(CultureEnum culture_)
        {
            if (CulturalNames == null)
            {
                CulturalNames = new Dictionary<CultureEnum, FullName>
                {
                    {CultureEnum.GB, new FullName (GB.Name, GB.LastName)},{CultureEnum.US, new FullName(US.Name ,US.LastName)},
                    {CultureEnum.IT, new FullName(IT.Name, IT.LastName)},{CultureEnum.ES, new FullName(ES.Name, ES.LastName)},
                    {CultureEnum.FR, new FullName(FR.Name, FR.LastName)},{CultureEnum.AE, new FullName(AE.Name, AE.LastName)},
                    {CultureEnum.DE, new FullName(DE.Name, DE.LastName)},{CultureEnum.NL, new FullName(NL.Name, NL.LastName)},
                    {CultureEnum.JP, new FullName(JP.Name, JP.LastName)},{CultureEnum.SE, new FullName(SE.Name, SE.LastName)},
                    {CultureEnum.IE, new FullName(IE.Name, IE.LastName)},{CultureEnum.AU, new FullName(AU.Name, AU.LastName)},
                    {CultureEnum.CA, new FullName(CA.Name, CA.LastName)},{CultureEnum.NZ, new FullName(NZ.Name, NZ.LastName)},
                    {CultureEnum.IN, new FullName(IN.Name, IN.LastName)},{CultureEnum.CN, new FullName(CN.Name, CN.LastName)}
                };
            }

            return CulturalNames[culture_];
        }


        [JsonProperty("AE")]
        public CultureNameDetails AE { get; set; }

        [JsonProperty("GB")]
        public CultureNameDetails GB { get; set; }

        [JsonProperty("US")]
        public CultureNameDetails US { get; set; }

        [JsonProperty("CN")]
        public CultureNameDetails CN { get; set; }

        [JsonProperty("IT")]
        public CultureNameDetails IT { get; set; }

        [JsonProperty("ES")]
        public CultureNameDetails ES { get; set; }

        [JsonProperty("FR")]
        public CultureNameDetails FR { get; set; }

        [JsonProperty("DE")]
        public CultureNameDetails DE { get; set; }

        [JsonProperty("NL")]
        public CultureNameDetails NL { get; set; }

        [JsonProperty("SE")]
        public CultureNameDetails SE { get; set; }

        [JsonProperty("IE")]
        public CultureNameDetails IE { get; set; }

        [JsonProperty("AU")]
        public CultureNameDetails AU { get; set; }

        [JsonProperty("CA")]
        public CultureNameDetails CA { get; set; }

        [JsonProperty("NZ")]
        public CultureNameDetails NZ { get; set; }

        [JsonProperty("IN")]
        public CultureNameDetails IN { get; set; }

        [JsonProperty("JP")]
        public CultureNameDetails JP { get; set; }
    }

    public partial class CultureNameDetails
    {
        [JsonProperty("Name")]
        public string Name { get; set; }

        [JsonProperty("LastName")]
        public string LastName { get; set; }
    }

    public partial class Cultures
    {
        [JsonProperty("Culture_UK")]
        public string Culture_UK { get; set; }

        [JsonProperty("Culture_US")]
        public string Culture_US { get; set; }

        [JsonProperty("Culture_CA")]
        public string Culture_CA { get; set; }

        [JsonProperty("Culture_DE")]
        public string Culture_DE { get; set; }

        [JsonProperty("Culture_JP")]
        public string Culture_JP { get; set; }
    }

}


JSON Data:
{
  "StringResources": {
    "Culture": {
      "Cultures": {
        "Culture_UK": "en-GB",
        "Culture_US": "en-US",
        "Culture_CA": "en-CA",
        "Culture_DE": "de-DE",
        "Culture_JP": "ja-JP"
      },
      "CulturalName": {
        "AE": {
          "Name": "Abu'l-Fadl",
          "LastName": "Isa"
        },
        "GB": {
          "Name": "Jamie",
          "LastName": "Lanister"
        },
        "US": {
          "Name": "Tyrion",
          "LastName": "Lanister"
        },
        "CN": {
          "Name": "陶",
          "LastName": "致远"
        },
        "IT": {
          "Name": "Clelia",
          "LastName": "Calabrese"
        },
        "ES": {
          "Name": "Sebastián",
          "LastName": "Hinojosa Argüello"
        },
        "FR": {
          "Name": "L'Heureux",
          "LastName": "Boulé"
        },
        "DE": {
          "Name": "Jörg",
          "LastName": "Gärtner"
        },
        "NL": {
          "Name": "Selma",
          "LastName": "Løkkeberg"
        },
        "SE": {
          "Name": "Ekström",
          "LastName": "Åberg"
        },
        "IE": {
          "Name": "Bailey",
          "LastName": "Quinn"
        },
        "AU": {
          "Name": "Lola",
          "LastName": "Patel"
        },
        "CA": {
          "Name": "Max",
          "LastName": "Godfrey"
        },
        "NZ": {
          "Name": "Edward",
          "LastName": "Bevan"
        },
        "IN": {
          "Name": "Anshu",
          "LastName": "Chahal"
        },
        "JP": {
          "Name": "ナルト",
          "LastName": "うずまき"
        }
      }
    }
  }
}

这就是我当前访问的方式


C#:
var StringResourceData = StringResourcesTestData.Get();
            var t1 = StringResourcesTestData.Get().Culture.CulturalName.GetName(Constants.Culture.GB);
            var t2 = StringResourcesTestData.Get().Culture.CulturalName.GetName(Constants.Culture.AE);
@跳伞 -对我的代码有何建议?在哪里可以实现Dictionary并将其包含在Json中?
 

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,533
地点
弗吉尼亚州切萨皮克
编程经验
10+
Apparently, there is no need to explictly use the StringEnumConverter. Json.NET will take care of things.

总的来说,我学会了 远离单身人士 如果可能的话,但您确实必须使用其中一种,以下是一种设置方法。

C#:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;

public enum CultureEnum
{
    GB,
    US,
    IT,
    FR,
    CA,
    DE,
    SE,
    NL,
    NZ,
    AU,
    AE,
    CN,
    JP,
    IE,
    IN,
    ES,
}

class FullName
{
    public string Name { get; set; }
    public string LastName { get; set; }

    public override string ToString() => $"{Name} {LastName}";
}

class Culture
{
    public IDictionary<string, string> Cultures { get; set; }
    public IDictionary<CultureEnum, FullName> CulturalName { get; set; }
}

class StringResources
{
    public Culture Culture { get; set; }

    public static StringResources Singleton { get; } = Load(@"StringResources3.json");

    public static StringResources Load(string jsonFile)
    {
        var jsonString = File.ReadAllText(jsonFile);
        var jobject = JObject.Parse(jsonString);
        return jobject["StringResources"].ToObject<StringResources>();
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine(StringResources.Singleton.Culture.CulturalName[CultureEnum.DE]);
        Console.WriteLine(StringResources.Singleton.Culture.Cultures["Culture_DE"]);
    }
}

这使用的是您上面发布的JSON数据。
StringResources3.json:
{
  "StringResources": {
    "Culture": {
      "Cultures": {
        "Culture_UK": "en-GB",
        "Culture_US": "en-US",
        "Culture_CA": "en-CA",
        "Culture_DE": "de-DE",
        "Culture_JP": "ja-JP"
      },
      "CulturalName": {
        "AE": {
          "Name": "Abu'l-Fadl",
          "LastName": "Isa"
        },
        "GB": {
          "Name": "Jamie",
          "LastName": "Lanister"
        },
        "US": {
          "Name": "Tyrion",
          "LastName": "Lanister"
        },
        "CN": {
          "Name": "陶",
          "LastName": "致远"
        },
        "IT": {
          "Name": "Clelia",
          "LastName": "Calabrese"
        },
        "ES": {
          "Name": "Sebastián",
          "LastName": "Hinojosa Argüello"
        },
        "FR": {
          "Name": "L'Heureux",
          "LastName": "Boulé"
        },
        "DE": {
          "Name": "Jörg",
          "LastName": "Gärtner"
        },
        "NL": {
          "Name": "Selma",
          "LastName": "Løkkeberg"
        },
        "SE": {
          "Name": "Ekström",
          "LastName": "Åberg"
        },
        "IE": {
          "Name": "Bailey",
          "LastName": "Quinn"
        },
        "AU": {
          "Name": "Lola",
          "LastName": "Patel"
        },
        "CA": {
          "Name": "Max",
          "LastName": "Godfrey"
        },
        "NZ": {
          "Name": "Edward",
          "LastName": "Bevan"
        },
        "IN": {
          "Name": "Anshu",
          "LastName": "Chahal"
        },
        "JP": {
          "Name": "ナルト",
          "LastName": "うずまき"
        }
      }
    }
  }
}
 
Last edited:

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,533
地点
弗吉尼亚州切萨皮克
编程经验
10+
The code above is fragile because the dictionary keys in theCulturalNames are dependent on the hardcoded CultureEnum. The following reads the JSON file at compile time and generates the enum, as well as the dictionaries. There is no need for the JSON file to exist at runtime.
StringResources.tt:
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="Newtonsoft.Json" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Newtonsoft.Json.Linq" #>
<#@ output extension=".cs" #>
<#
    var jsonFile = this.Host.ResolvePath("StringResources3.json");
    var jsonString = File.ReadAllText(jsonFile);
    var jobject = JObject.Parse(jsonString);
    var cultures = jobject["StringResources"]["Culture"]["Cultures"].ToObject<Dictionary<string,string>>();
    var dictionary = jobject["StringResources"]["Culture"]["CulturalName"].ToObject<Dictionary<string,JToken>>();
#>

using System.Collections.Generic;

enum CultureEnum
{
<#
    PushIndent("    ");
    foreach(var key in dictionary.Keys)
        WriteLine($"{key},");
    PopIndent();
#>
}

class FullName
{
    public string Name { get; set; }
    public string LastName { get; set; }

    public override string ToString() => $"{Name} {LastName}";
}

class Culture
{
    public readonly IReadOnlyDictionary<string, string> Cultures = new Dictionary<string, string>()
    {
<#
    foreach(var kvp in cultures)
    {
#>
        ["<#= kvp.Key #>"] = "<#= kvp.Value #>",
<#
    }
#>
    };

    public readonly IReadOnlyDictionary<CultureEnum, FullName> CulturalName = new Dictionary<CultureEnum, FullName>()
    {
<#
    foreach(var kvp in dictionary)
    {
        string name = kvp.Value["Name"].ToString();
        string lastName = kvp.Value["LastName"].ToString();
#>
        [CultureEnum.<#= kvp.Key #>] = new FullName() { Name = "<#= name #>", LastName = "<#= lastName #>" },
<#
    }
#>
    };
}

static class StringResources
{
    public static Culture Culture { get; } = new Culture();
}

生成以下代码:
StringResources.cs:
using System.Collections.Generic;

enum CultureEnum
{
    AE,
    GB,
    US,
    CN,
    IT,
    ES,
    FR,
    DE,
    NL,
    SE,
    IE,
    AU,
    CA,
    NZ,
    IN,
    JP,
}

class FullName
{
    public string Name { get; set; }
    public string LastName { get; set; }

    public override string ToString() => $"{Name} {LastName}";
}

class Culture
{
    public readonly IReadOnlyDictionary<string, string> Cultures = new Dictionary<string, string>()
    {
        ["Culture_UK"] = "en-GB",
        ["Culture_US"] = "en-US",
        ["Culture_CA"] = "en-CA",
        ["Culture_DE"] = "de-DE",
        ["Culture_JP"] = "ja-JP",
    };

    public readonly IReadOnlyDictionary<CultureEnum, FullName> CulturalName = new Dictionary<CultureEnum, FullName>()
    {
        [CultureEnum.AE] = new FullName() { Name = "Abu'l-Fadl", LastName = "Isa" },
        [CultureEnum.GB] = new FullName() { Name = "Jamie", LastName = "Lanister" },
        [CultureEnum.US] = new FullName() { Name = "Tyrion", LastName = "Lanister" },
        [CultureEnum.CN] = new FullName() { Name = "陶", LastName = "致远" },
        [CultureEnum.IT] = new FullName() { Name = "Clelia", LastName = "Calabrese" },
        [CultureEnum.ES] = new FullName() { Name = "Sebastián", LastName = "Hinojosa Argüello" },
        [CultureEnum.FR] = new FullName() { Name = "L'Heureux", LastName = "Boulé" },
        [CultureEnum.DE] = new FullName() { Name = "Jörg", LastName = "Gärtner" },
        [CultureEnum.NL] = new FullName() { Name = "Selma", LastName = "Løkkeberg" },
        [CultureEnum.SE] = new FullName() { Name = "Ekström", LastName = "Åberg" },
        [CultureEnum.IE] = new FullName() { Name = "Bailey", LastName = "Quinn" },
        [CultureEnum.AU] = new FullName() { Name = "Lola", LastName = "Patel" },
        [CultureEnum.CA] = new FullName() { Name = "Max", LastName = "Godfrey" },
        [CultureEnum.NZ] = new FullName() { Name = "Edward", LastName = "Bevan" },
        [CultureEnum.IN] = new FullName() { Name = "Anshu", LastName = "Chahal" },
        [CultureEnum.JP] = new FullName() { Name = "ナルト", LastName = "うずまき" },
    };
}

static class StringResources
{
    public static Culture Culture { get; } = new Culture();
}

可以通过以下方式调用:
Program.cs:
class Program
{
    static void Main()
    {
        Console.WriteLine(StringResources.Culture.CulturalName[CultureEnum.DE]);
        Console.WriteLine(StringResources.Culture.Cultures["Culture_DE"]);
    }
}

这使用相同的"StringResources3.json",我以前一直在使用。
 
最佳 底部