c#从字符串中提取某些单词
当我得到http响应时,它如下所示:c#从字符串中提取某些单词,c#,extract,C#,Extract,当我得到http响应时,它如下所示: { "course_editions": { "2014/SL": [ { "course_id": "06-DEGZLI0", "term_id": "2014/SL", "course_name": { "en": "Preparation for bachelor exam", } }, { "course_id": "06-DPRALW0", "term_id": "2014/S
{
"course_editions": {
"2014/SL": [
{
"course_id": "06-DEGZLI0",
"term_id": "2014/SL",
"course_name": {
"en": "Preparation for bachelor exam",
}
},
{
"course_id": "06-DPRALW0",
"term_id": "2014/SL",
"course_name": {
"en": "Work experience",
}
},
{
我只想提取课程名称,f.e.:
Work experience
Preparation for bachelor exam
我试过这个:
string probably_json = GetResponse(url_courses);
object obj = JsonConvert.DeserializeObject(probably_json);
using (StringReader reader = new StringReader(obj.ToString().Replace("\\t", " ").Replace("\\n", "\n")))
{
string line;
int lineNo = 0;
while ((line = reader.ReadLine()) != null)
{
if (line.Contains("en"))
{
string output = line.Substring(0, line.Length-1);
Console.WriteLine(output);
}
++lineNo;
}
} // End Using StreamReader
但我只有这些:
"en": "Preparation for bachelor exam" "en": "Work experience"
我应该怎么做,只获取课程标题?如果您正在使用json.net,请让它做一些工作,不要解析自己:
var result = JObject
.Parse(probably_json)
.SelectTokens("['course_editions'].['2014/SL'].[*].['course_name'].['en']");
为什么不能检查反序列化对象返回的属性?请看这个答案,它解释了如何将JsonConvert与动态对象一起使用。您不需要使用StringReader,只需调用obj.course\u name.en即可