将 JSON 对象加载到数组中?
Posted
技术标签:
【中文标题】将 JSON 对象加载到数组中?【英文标题】:Load JSON object into array? 【发布时间】:2021-10-24 22:16:27 【问题描述】:我无法将此 json 对象加载到数组中。
这是我从 API 获得的 JSON 响应的 sn-p(片段):
"count":192,
"value":[
"id":"03dd9f56-108f-4e8f-b92e-93df05717464",
"name":"IIBTest",
"url":"https://devops.com/tfs/DefaultCollection/_apis/projects/03dd9f56-108f-4e8f-b92e-93df05717464",
"state":"wellFormed",
"revision":14434848,
"visibility":"private",
"lastUpdateTime":"2016-08-19T12:21:37.187Z"
,
"id":"b7e15034-fc8f-4f7e-866a-cb06f44b12ed",
"name":"MS Project POC",
"description":"POC for MS Project with TFS",
"url":"https://devops.com/tfs/DefaultCollection/_apis/projects/b7e15034-fc8f-4f7e-866a-cb06f44b12ed",
"state":"wellFormed",
"revision":14434955,
"visibility":"private",
"lastUpdateTime":"2017-10-03T19:31:56.56Z"
,
"id":"59e06621-c5f5-4fd1-9c55-1def541b99d9",
"name":"WorkflowReporting",
"url":"https://devops.com/tfs/DefaultCollection/_apis/projects/59e06621-c5f5-4fd1-9c55-1def541b99d9",
"state":"wellFormed",
"revision":14434591,
"visibility":"private",
"lastUpdateTime":"2015-09-11T06:59:12.21Z"
,
"id":"78a802f0-5eee-4bcb-bde9-a764e46f56db",
"name":"iSolutions",
"description":"",
"url":"https://devops.com/tfs/DefaultCollection/_apis/projects/78a802f0-5eee-4bcb-bde9-a764e46f56db",
"state":"wellFormed",
"revision":14435476,
"visibility":"private",
"lastUpdateTime":"2021-08-05T17:17:26.193Z"
,
"id":"1f20506a-63a5-486a-a857-fec64d7486a6",
"name":"Training",
"description":"MLITS Training and Learning",
"url":"https://devops.com/tfs/DefaultCollection/_apis/projects/1f20506a-63a5-486a-a857-fec64d7486a6",
"state":"wellFormed",
"revision":14435350,
"visibility":"private",
"lastUpdateTime":"2021-04-08T22:48:02.923Z"
,
...
这是我的代码:
public class Rootobject
public int count get; set;
public Value[] value get; set;
public class Value
public string id get; set;
public string name get; set;
public string url get; set;
public string state get; set;
public int revision get; set;
public string visibility get; set;
public DateTime lastUpdateTime get; set;
public string description get; set;
static void Main(string[] args)
var client = new RestClient("https://devops.americannational.com/tfs/defaultcollection/_apis/projects?$top=300&api-version=5.0")
Authenticator = new RestSharp.Authenticators.NtlmAuthenticator()
;
client.Timeout = -1;
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
var jsonString = response.Content;
var jo = JObject.Parse(jsonString);
//...
我想将项目的名称加载到数组中,以便以后可以遍历它们。任何帮助表示赞赏,我已经尝试了一些东西,但运气不佳。
【问题讨论】:
你试过GetAsync<Rootobject>()
而不是client.Execute
吗? RestSharp 文档:restsharp.dev/getting-started/…
运行时会发生什么?是否抛出异常?如果是这样,请将异常详细信息添加到您的问题中。
你得到成功的HTTP响应码了吗?
【参考方案1】:
你可以使用 var jo = JsonConvert.DeserializeObject<Rootobject>(jsonString);
然后遍历jo.value
【讨论】:
@ShahramH.Farmanesh 如果您认为这是正确的答案,请为帖子的作者点赞。【参考方案2】:试试这个
var jsonDeserialized= JsonConvert.DeserializeObject<Rootobject>(json);
作为示例如何使用它,此查询返回项目名称数组
var projectNames= jsonDeserialized.value.Select(v => v.name ).ToArray();
输出
["IIBTest","MS Project POC","WorkflowReporting","iSolutions","Training"]
这会返回一个项目列表
var projects= jsonDeserialized.value.ToList();
【讨论】:
谢谢你这个作品,虽然我不明白这部分,(v => v.name)。你能解释一下吗? @ShaneP 我只是在创建项目名称列表。查看我的更新答案以上是关于将 JSON 对象加载到数组中?的主要内容,如果未能解决你的问题,请参考以下文章