gpt4 book ai didi

C# JToken.SelectTokens 方法 - 什么 JPath 表达式?

转载 作者:太空狗 更新时间:2023-10-29 20:30:23 26 4
gpt4 key购买 nike

我有这样的 json 文本:

{
"course_editions": {
"2014/SL": [
{
"grades": {
"course_units_grades": {
"159715": {
"1": {
"value_symbol": "4",
"exam_session_number": 1,
"exam_id": 198172,
"value_description": {
"en": "good",
}
}
}
},
"course_grades": {}
}
},
{
"grades": {
"course_units_grades": {
"159796": {
"1": {
"value_symbol": "5",
"exam_session_number": 1,
"exam_id": 198259,
"value_description": {
"en": "very good",
}
}
}
},
"course_grades": {}
}
},

我想使用命名空间中的 JToken.SelectTokens 方法:Newtonsoft.Json.Linq

我试过这样的:

string json_response = GetResponse(sign(url_courses));
var courses_tokens = JObject.Parse(json_response).SelectTokens("['course_editions'].['2014/SL'].[*].['grades'].*")

这是行不通的。我只想在 course_unit_grades 之后和 “1” 之前获得这些数字。所以在这个例子中只有:"159715""159796" 才能使用它们,在

中一一使用
foreach(var lp in courses_tokens) {
}

最佳答案

这是一种可能的方式:

var jobj = JObject.Parse(json);
var coursesTokens = jobj.SelectTokens("course_editions.2014/SL[*].grades.course_units_grades")
.Select(o => o.First) //get the first child of `course_units_grades`
.Cast<JProperty>() //cast to JProperty
.Select(o => o.Name); //get the name of the property
foreach (string coursesToken in coursesTokens)
{
Console.WriteLine(coursesToken);
}

Dotnetfiddle Demo

给定底部的 json 示例,输出如下:

159715
159796

json 示例:

var json = @"{
'course_editions': {
'2014/SL': [
{
'grades': {
'course_units_grades': {
'159715': {
'1': {
'value_symbol': '4',
'exam_session_number': 1,
'exam_id': 198172,
'value_description': {
'en': 'good'
}
}
}
},
'course_grades': {}
}
},
{
'grades': {
'course_units_grades': {
'159796': {
'1': {
'value_symbol': '5',
'exam_session_number': 1,
'exam_id': 198259,
'value_description': {
'en': 'very good'
}
}
}
},
'course_grades': {}
}
}
]
}
}";

关于C# JToken.SelectTokens 方法 - 什么 JPath 表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31367434/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com