Newtonsoft.Json.Linq.JArray.Parse(string)' 有一些无效参数
Newtonsoft.Json.Linq.JArray.Parse(string)' has some invalid arguments
我正在尝试使用 API's 从 D2L 中提取数据以用于保管箱提交 我有一个 link 其中 returns 一个 Json 数组 [= =13=] 从这个数组中我只需要 Id 字段。
我曾尝试将此数组转换为动态对象,但这没有帮助。
这是我的代码。
var client = new RestClient("https://" + LMS_URL);
var authenticator = new ValenceAuthenticator(userContext);
string Link = "/d2l/api/le/1.12/UnitID/dropbox/folders/UniID/submissions/";
var request = new RestRequest(string.Format(Link));
request.Method = Method.GET;
authenticator.Authenticate(client, request);
var response = client.Execute(request);
string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(response.Content);
Response.Write(jsonString);
//var splashInfo = JsonConvert.DeserializeObject<ObjectD>(response.Content);
dynamic jsonResponse = JsonConvert.DeserializeObject(response.Content);
var parsedObject = jsonResponse.Parse(jsonResponse);
string json = jsonResponse;
var popupJson = parsedObject["id"].ToString();
我的目标是从这个响应中获取 ID 列表并循环遍历它们,这些 ID 是我下一个 API 路线的关键。
下面是我从回复中得到的信息:
[
{
"Id": 2021,
"CategoryId": null,
"Name": "Graded Assignment: Part 1",
"CustomInstructions": {
"Text": "Directions:\r\nCOMPLETE the following TestOut Activities\r\n\r\n1.2.10 Practice Questions\r\n1.3.8 Practice Questions\r\n\r\nGo to TestOut to complete the Assignment."
//Other properties omitted
}
//Other properties omitted
}
]
返回的JSON中最外层的容器是一个数组,所以需要反序列化为Newtonsoft的Serialization Guide: IEnumerable, Lists, and Arrays中指定的集合类型。
因为您只关心数组中对象的 Id
属性,您可以使用 JsonConvert.DeserializeAnonymousType
来反序列化有趣的值:
var ids = JsonConvert.DeserializeAnonymousType(response.Content, new [] { new { Id = default(long) } })
.Select(o => o.Id)
.ToList();
如果你确定最外层的数组只包含一项,你可以这样做:
var id = JsonConvert.DeserializeAnonymousType(response.Content, new [] { new { Id = default(long) } })
.Select(o => o.Id)
.Single();
或者,如果您认为以后需要反序列化其他属性,您可以创建一个显式数据模型,如下所示:
public class ObjectId
{
public long Id { get; set; }
}
然后做:
var ids = JsonConvert.DeserializeObject<List<ObjectId>>(response.Content)
.Select(o => o.Id)
.ToList();
备注:
您需要根据 API 文档确定 long
或 int
是否更适合 Id
值。
作为一般规则,我建议不要解析为 dynamic
,因为您会丢失编译时的正确性检查。在处理完全自由格式的 JSON 时,解析为 JToken
可能是更好的解决方案——但是您的 JSON 似乎具有固定的架构,因此两者都没有必要。
演示 fiddle here.
我正在尝试使用 API's 从 D2L 中提取数据以用于保管箱提交 我有一个 link 其中 returns 一个 Json 数组 [= =13=] 从这个数组中我只需要 Id 字段。
我曾尝试将此数组转换为动态对象,但这没有帮助。
这是我的代码。
var client = new RestClient("https://" + LMS_URL);
var authenticator = new ValenceAuthenticator(userContext);
string Link = "/d2l/api/le/1.12/UnitID/dropbox/folders/UniID/submissions/";
var request = new RestRequest(string.Format(Link));
request.Method = Method.GET;
authenticator.Authenticate(client, request);
var response = client.Execute(request);
string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(response.Content);
Response.Write(jsonString);
//var splashInfo = JsonConvert.DeserializeObject<ObjectD>(response.Content);
dynamic jsonResponse = JsonConvert.DeserializeObject(response.Content);
var parsedObject = jsonResponse.Parse(jsonResponse);
string json = jsonResponse;
var popupJson = parsedObject["id"].ToString();
我的目标是从这个响应中获取 ID 列表并循环遍历它们,这些 ID 是我下一个 API 路线的关键。
下面是我从回复中得到的信息:
[
{
"Id": 2021,
"CategoryId": null,
"Name": "Graded Assignment: Part 1",
"CustomInstructions": {
"Text": "Directions:\r\nCOMPLETE the following TestOut Activities\r\n\r\n1.2.10 Practice Questions\r\n1.3.8 Practice Questions\r\n\r\nGo to TestOut to complete the Assignment."
//Other properties omitted
}
//Other properties omitted
}
]
返回的JSON中最外层的容器是一个数组,所以需要反序列化为Newtonsoft的Serialization Guide: IEnumerable, Lists, and Arrays中指定的集合类型。
因为您只关心数组中对象的 Id
属性,您可以使用 JsonConvert.DeserializeAnonymousType
来反序列化有趣的值:
var ids = JsonConvert.DeserializeAnonymousType(response.Content, new [] { new { Id = default(long) } })
.Select(o => o.Id)
.ToList();
如果你确定最外层的数组只包含一项,你可以这样做:
var id = JsonConvert.DeserializeAnonymousType(response.Content, new [] { new { Id = default(long) } })
.Select(o => o.Id)
.Single();
或者,如果您认为以后需要反序列化其他属性,您可以创建一个显式数据模型,如下所示:
public class ObjectId
{
public long Id { get; set; }
}
然后做:
var ids = JsonConvert.DeserializeObject<List<ObjectId>>(response.Content)
.Select(o => o.Id)
.ToList();
备注:
您需要根据 API 文档确定
long
或int
是否更适合Id
值。作为一般规则,我建议不要解析为
dynamic
,因为您会丢失编译时的正确性检查。在处理完全自由格式的 JSON 时,解析为JToken
可能是更好的解决方案——但是您的 JSON 似乎具有固定的架构,因此两者都没有必要。
演示 fiddle here.