我在Visual Studio中有一个C#项目,我正在使用JSON.net,我有一个包含5个对象的JSON文件。我希望当我单击按钮时,来自5个对象的特定数据(例如“名称”和“描述”)会显示在文本框中。
我不知道使用什么方法,因此我可以访问JSON文件并获取其中的数据。
我在网上阅读到JSON只是格式化数据的一种方式,如果我要执行类似查询的操作,则应该使用数据库。
这是JSON文件:
{
"Fighter Features":{
"Fighting Style (Archery)":{
"name":"Fighting Style (Archery)",
"Description:":"You gain +2 bonus to attack rolls you make with ranged weapons."
},
"Second Wind":{
"name":"Second Wind",
"Description:":"Vou have a limited well of stamina that you can draw on to protect Yourself from harm. On your turn, you can use a bonus action to regain hit points equal to ld10 + your fighter leveI."
},
"Fighthing Style (Defense)":{
"name":"Fighting Style (Defense)",
"Description:":"While you are wearing armor you gain a +1 bonus to AC."
}
}
}
答案 0 :(得分:1)
我将使用File.ReadAllText方法在您的计算机路径中获取JSON数据。
string jsonData= File.ReadAllText("your file path");
然后使用json.net JsonConvert.DeserializeObject
方法将json反序列化为对象。
RootObject jsonObj = JsonConvert.DeserializeObject<RootObject>(jsonData);
答案 1 :(得分:0)
您可以使用它。要使用此功能,请使用Newtonsoft.Json(可以作为Nuget软件包安装)。
public partial class Welcome
{
[JsonProperty("Fighter Features")]
public FighterFeatures FighterFeatures { get; set; }
}
public partial class FighterFeatures
{
[JsonProperty("Fighting Style (Archery)")]
public FighthingStyleDefense FightingStyleArchery { get; set; }
[JsonProperty("Second Wind")]
public FighthingStyleDefense SecondWind { get; set; }
[JsonProperty("Fighthing Style (Defense)")]
public FighthingStyleDefense FighthingStyleDefense { get; set; }
}
public partial class FighthingStyleDefense
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("Description:")]
public string Description { get; set; }
}
public partial class Welcome
{
public static Welcome FromJson(string json) => JsonConvert.DeserializeObject<Welcome>(json, Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Welcome self) => JsonConvert.SerializeObject(self, Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters = {
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
答案 2 :(得分:0)
如果我对您的理解正确,那么您希望像使用强类型对象一样使用JSON字符串。在这种情况下,来自Json.net的JObject是另一个选择。基本上,JObject是一个递归容器,您可以递归遍历其中的所有属性。 例如:
void Main()
{
string nameOfFeature = "Second Wind";
JObject jObject = JObject.Parse(data)["Fighter Features"] as JObject;
foreach (JProperty feature in jObject.Properties())
{
if (feature.Name == nameOfFeature)
{
JObject secondWind = feature.Value as JObject;
foreach (JProperty fProperty in secondWind.Properties())
{
Console.WriteLine (fProperty.Name + ": " + fProperty.Value);
}
}
}
}
如果您不介意更改Json数据结构,一种更干净的方法是创建一个强类型模型,该模型映射JSON字符串的属性,以便您可以使用DeserializeObject将JSON字符串转换为C#模型。欲望。 例如:
void Main()
{
FighterFigure fighterFeature = JsonConvert.DeserializeObject<List<FighterFigure>>(data);
}
class FighterFigure
{
public string Name { get; set; }
public Feature Feature { get; set; }
}
class Feature
{
public string Name { get; set; }
public string Description { get; set; }
}
static string data = @"
[
{
'Name': 'Fighting Style (Archery)',
'Feature': {
'name': 'Fighting Style (Archery)',
'Description': 'You gain +2 bonus to attack rolls you make with ranged weapons.'
},
},
{
'Name': 'Second Wind',
'Feature': {
'name': 'Second Wind',
'Description': 'You have a limited well of stamina that you can draw on to protect Yourself from harm. On your turn, you can use a bonus action to regain hit points equal to ld10 + your fighter leveI.'
},
},
{
'Name': 'Fighthing Style (Defense)',
'Feature': {
'name': 'Fighthing Style (Defense)',
'Description': 'While you are wearing armor you gain a +1 bonus to AC.'
},
}
]";