C# 在json文件中循环
我有一个json结果,我正在尝试循环,但它似乎退出了我的foreach,这是我的json结果C# 在json文件中循环,c#,C#,我有一个json结果,我正在尝试循环,但它似乎退出了我的foreach,这是我的json结果 { "word": "example", "results": [{ "definition": "a representative form or pattern", "partOfSpeech": "noun", "synonyms": ["model"], "typeOf": ["representation", "i
{
"word": "example",
"results": [{
"definition": "a representative form or pattern",
"partOfSpeech": "noun",
"synonyms": ["model"],
"typeOf": ["representation", "internal representation", "mental representation"],
"hasTypes": ["prefiguration", "archetype", "epitome", "guide", "holotype", "image", "loadstar", "lodestar", "microcosm", "original", "paradigm", "pilot", "prototype", "template", "templet", "type specimen"],
"derivation": ["exemplify"],
"examples": ["I profited from his example"]
}, {
"definition": "something to be imitated",
"partOfSpeech": "noun",
"synonyms": ["exemplar", "good example", "model"],
"typeOf": ["ideal"],
"hasTypes": ["pacemaker", "pattern", "beauty", "prodigy", "beaut", "pacesetter"],
"derivation": ["exemplify", "exemplary"]
}, {
"definition": "an occurrence of something",
"partOfSpeech": "noun",
"synonyms": ["case", "instance"],
"typeOf": ["happening", "natural event", "occurrence", "occurrent"],
"hasTypes": ["clip", "mortification", "piece", "time", "humiliation", "bit"],
"derivation": ["exemplify"],
"examples": ["but there is always the famous example of the Smiths"]
}, {
"definition": "an item of information that is typical of a class or group",
"partOfSpeech": "noun",
"synonyms": ["illustration", "instance", "representative"],
"typeOf": ["information"],
"hasTypes": ["excuse", "apology", "specimen", "case in point", "sample", "exception", "quintessence", "precedent"],
"derivation": ["exemplify", "exemplary"],
"examples": ["this patient provides a typical example of the syndrome", "there is an example on page 10"]
}, {
"definition": "punishment intended as a warning to others",
"partOfSpeech": "noun",
"synonyms": ["deterrent example", "lesson", "object lesson"],
"typeOf": ["monition", "admonition", "word of advice", "warning"],
"derivation": ["exemplary"],
"examples": ["they decided to make an example of him"]
}, {
"definition": "a task performed or problem solved in order to develop skill or understanding",
"partOfSpeech": "noun",
"synonyms": ["exercise"],
"typeOf": ["lesson"],
"examples": ["you must work the examples at the end of each chapter in the textbook"]
}],
"syllables": {
"count": 3,
"list": ["ex", "am", "ple"]
},
"pronunciation": {
"all": "ɪɡ'zæmpəl"
},
"frequency": 4.67
}
下面的代码,将json字符串放入一个动态变量,以循环结果(至少我正在尝试)
dynamic words = JObject.Parse(responseString);
if (words.results != null)
{
try
{
foreach (dynamic example in words.results)
{
MessageBox.Show("result");
if (example.partOfSpeech != null)
{
builder.AppendText("Type");
builder.AppendBreak(new TimeSpan(10000000));
builder.AppendText(example.partOfSpeech);
builder.AppendBreak(new TimeSpan(10000000));
}
MessageBox.Show("result");
if (example.definition != null)
{
builder.AppendText("Definition");
builder.AppendBreak(new TimeSpan(10000000));
builder.AppendText(example.definition);
builder.AppendBreak(new TimeSpan(10000000));
}
MessageBox.Show("result");
if (example.examples != null)
{
builder.AppendText("Example sentence");
builder.AppendBreak(new TimeSpan(10000000));
builder.AppendText(example.examples[0]);
}
MessageBox.Show("result");
}
}
}
我正试图循环遍历每个结果,但它似乎并没有用我的代码来完成,怎么可能呢
我循环遍历结果?words.results是否返回空值?您的try内容是否引发异常?请在此帮助我们,值得一提的是,该代码适用于我-减去它正确遍历的构建器。@CallumBradbury我收到以下错误:这是构建器的问题,而不是json的问题。也许可以隔离构建呃,问一个关于that@CallumBradbury这不可能,因为如果我把它放在一个消息框中,我也会得到同样的错误