我有这种格式的json文件
{
"myserver": {
"readonly": false,
"slaveonly": false
},
"dateChanged": true,
"pid": "3875",
"data-mode": {
"version": 461329,
"mode": "delta",
"Jobs" : {
"job-mode": "full",
"1" : { id : 100, description : "descritpion 1" },
"2" : { id : 200, description : "descritpion 1" },
"3" : { id : 300, description : "descritpion 1" }
}
}
}
我有一堂课叫乔布
public class Job
{
public string Id {get;set;}
public string Desc {get;set;}
}
我需要所有工作的清单,作为json字符串中的IList。
我需要读取Jobs标记(1,2,3)中的所有值。我在这里做了一些工作。
using (StreamReader r = new StreamReader(@"10022017.json"))
{
string json = r.ReadToEnd();
dynamic obj = JsonConvert.DeserializeObject(json);
//obj.
// var xx = ((Newtonsoft.Json.Linq.JArray)((Newtonsoft.Json.Linq.JProperty)(new System.Collections.Generic.Mscorlib_CollectionDebugView<Newtonsoft.Json.Linq.JToken>(((Newtonsoft.Json.Linq.JObject)((Newtonsoft.Json.Linq.JProperty)(new System.Collections.Generic.Mscorlib_CollectionDebugView<Newtonsoft.Json.Linq.JToken>(((Newtonsoft.Json.Linq.JObject)obj).ChildrenTokens).Items[14])).Value).ChildrenTokens).Items[6])).Value).ChildrenTokens;
var jObj = JObject.Parse(json);
var data = jObj.Descendants()
.OfType<JProperty>()
.Where(p => p.Name == "jobs");
foreach (var item in data.Descendants())
{
}
}
不知道如何获取每一行的价值并将其转换为某个类。
帮助将不胜感激。
谢谢
是的,您的错误是“工作” | “职位”..
您可以用作替代。
var jObj = JObject.Parse(jsonText); ;
var jobs = jObj["data-mode"]["Jobs"];
var result = jobs.OfType<JProperty>().Where((a,b)=>{
int key = 0;
return int.TryParse(a.Name,out key);
}).Select<JProperty,Job>((jp,i) => {
return new Job
{
Id = jp.Value["id"].ToObject<string>(),
Desc = jp.Value["description"].ToObject<string>()
};
}).ToArray();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句