【发布时间】:2020-12-01 01:10:01
【问题描述】:
我正在尝试设置一个变量 def,以便在定义类中包含定义的值 这是我要引用的类。
public class Definition
{
public string type { get; set; }
//need to get this to def in PlayButton event
public string definition { get; set; }
public string example { get; set; }
public string image_url { get; set; }
public object emoji { get; set; }
}
public class Root
{
public List<Definition> definitions { get; set; }
public string word { get; set; }
public string pronunciation { get; set; }
}
我需要在事件中包含 def 变量,但我不知道怎么做,为此我必须引用 DeserializedJSON 变量,因为 JSON 正在从那里解析到该类中,但我找不到方法从DeserializedJSON 获取它到defs:
//Click PlayButton event
public async void PlayButton_Click(object sender, MouseButtonEventArgs e)
{
//Changing From Start Screen Grid To Quiz Screen Grid
QuizScreen.Visibility = Visibility.Visible;
StartScreen.Visibility = Visibility.Collapsed;
//Running game logic once to start game up
string word = RandomWord();
definitionUnformatted = await Define(word);
//Deserialising JSON
Root DeserializedJSON = JsonConvert.DeserializeObject<Root>(definitionUnformatted);
string def = //help here pls
DefinitionBox.Text = def;
}
感谢您的帮助,我只是希望这是一个简单的疏忽,我可以从中学习。
编辑:@crowcoder 要求提供 JSON 的示例
{
"definitions": [
{
"type": "noun",
"definition": "something shaped like a platter, in particular:",
"example": "dig out some old platters",
"image_url": null,
"emoji": null
},
{
"type": "noun",
"definition": "a large flat dish or plate for serving food.",
"example": "arrange the fruit on a serving platter",
"image_url": "https://media.owlbot.info/dictionary/images/kkkki.jpg.400x400_q85_box-0,0,360,360_crop_detail.jpg",
"emoji": null
}
],
"word": "platter",
"pronunciation": "ˈpladər"
}
【问题讨论】:
-
根据你的班级,你得到一个根对象,它有一个定义列表,你想要哪个?
-
你的
Root类有一个属性definitions,它的类型是List<Definition>,你想从这个列表的哪个项目中获取definition的值? -
您能否展示您尝试反序列化的 JSON 的相关示例?
-
@thatguy 我不确定你的意思,我希望将定义的值分配给“playbutton_click”中的变量
-
@AthulRaj 我想将“定义”类中的“公共字符串定义”的值放入“playbutton_click”中的变量中