使用 c# 递归解析 JSON
Posted
技术标签:
【中文标题】使用 c# 递归解析 JSON【英文标题】:Recursive parse JSON with c# 【发布时间】:2020-05-31 01:55:06 【问题描述】:我尝试进行递归解析,但它不起作用,它只解析第一个标题。
我已经尝试过:C# parse recursive json,但它不起作用。
我也尝试使用 Regex 进行解析,但它也不起作用,它只捕获第一个标题。
所以我希望你能帮助我解决阻碍我进步的问题:/
我的杰森
"data":
"count":[
],
"list":[
"title":"new doc 4",
"rotate":0,
"sort_key":"new doc 4",
"tag_ids":"",
"doc_id":"ee4DM4Ly7CFBM3JFWAW60TUX",
"co_token":"",
"p":"XQXLDEQyA2hf6BBfyXhSaUHL",
"t":"1474932063",
"c":"2",
"updated":"1474932063"
,
"title":"new doc 5",
"rotate":0,
"sort_key":"new doc 5",
"tag_ids":"",
"doc_id":"Xy67QdRhTR9XS159WLyCCTbK",
"co_token":"",
"p":"XadS23UUQbQRQt9gLPWDWTAQ",
"t":"1474932060",
"c":"1",
"updated":"1474932061"
,
"title":"new doc 6",
"rotate":0,
"sort_key":"new doc 6",
"tag_ids":"",
"doc_id":"Q4W55XLA1AeERUJHaVN7EF80",
"co_token":"",
"p":"T6BYAMKXNa086Tb4FaYd4rV1",
"t":"1474932059",
"c":"1",
"updated":"1474932059"
,
"title":"new doc 7",
"rotate":0,
"sort_key":"new doc 7",
"tag_ids":"",
"doc_id":"9heQFfeYFUFXb536VTyHLhKL",
"co_token":"",
"p":"BeFULN12QL6H9L5HXCAYfH1S",
"t":"1474932056",
"c":"2",
"updated":"1474932056"
,
"title":"new doc 8",
"rotate":0,
"sort_key":"new doc 8",
"tag_ids":"",
"doc_id":"H7eXd1yTfFAY2V8ha3a6FS9K",
"co_token":"",
"p":"LJVyNVCPMbXH2abMMbb6BRYN",
"t":"1474932053",
"c":"1",
"updated":"1474932053"
,
"title":"new doc 9",
"rotate":0,
"sort_key":"new doc 9",
"tag_ids":"",
"doc_id":"3VVL56tQDXf73V8UKXrNX0d0",
"co_token":"",
"p":"rV2H7WWCRy1Vrb0PaU1TQKTD",
"t":"1474932047",
"c":"3",
"updated":"1474932049"
,
"title":"new doc 10",
"rotate":0,
"sort_key":"new doc 10",
"tag_ids":"",
"doc_id":"4TBabHAKNRXdREJXNdWfQEWF",
"co_token":"",
"p":"TR7Dt89gV3hfSJBTDQ1JQP72",
"t":"1474402937",
"c":"1",
"updated":"1474402937"
,
"title":"new doc 11",
"rotate":0,
"sort_key":"new doc 11",
"tag_ids":"",
"doc_id":"TV4fBdehY4fFHN00g082QDKX",
"co_token":"",
"p":"SCUPQ9bW6BgTT9JAP4K2WCYU",
"t":"1474402932",
"c":"3",
"updated":"1474402932"
,
"title":"new doc 12",
"rotate":0,
"sort_key":"new doc 12",
"tag_ids":"",
"doc_id":"M8fHK8gQB3FWUEeLKQMdUaFB",
"co_token":"",
"p":"BPDTg3aTTDELyFUA1WK0M2rA",
"t":"1474402911",
"c":"7",
"updated":"1474402913"
,
"title":"new doc",
"rotate":0,
"sort_key":"new doc",
"tag_ids":"",
"doc_id":"ayCK8RrHSe796g4PSNRgMD5N",
"co_token":"",
"p":"Y46RWJFb0XJRHtKy6B077Me1",
"t":"1389379718",
"c":"1",
"updated":"1389379718"
],
【问题讨论】:
你没有递归。这看起来只是一个列表。 你的 json 没有任何需要你做任何递归的事情......JObject.Parse(json)
将负责反序列化。另外,请解释一下你打算如何在解析后使用这个 json
我想在文本文件中添加标题
我不明白如何:JObject.Parse(json) 可以帮助我找回标题。
【参考方案1】:
-
将字符串解析为 c# 可用于迭代的对象。
然后使用 foreach 循环访问列表中的项目。
为字符串生成器添加标题
将收集的标题保存到文件中。
var obj = JObject.Parse(jsonString);
var list = obj["data"]["list"];
StringBuilder sb = new StringBuilder();
foreach(var item in list)
sb.AppendLine(item["title"].ToString());
File.WriteAllText(@"c:\temp\titles.txt", sb.ToString());
【讨论】:
sb.ToString() 上的错误; C# 参数 2:无法从 'string' 转换为 'string[]' 使用 WriteAllText 代替行。【参考方案2】:我使用 Newtonsoft.Json 包从包含 json 数据的文件 data.json 转换 json
class Program
static void Main(string[] args)
LoadJson();
public static void LoadJson()
using (StreamReader r = new StreamReader(@"C:\\Users\\user\\source\\repos\\RecursiveParseJSON\\RecursiveParseJSON\\data.json"))
string json = r.ReadToEnd();
JsonObject datas = JsonConvert.DeserializeObject<JsonObject>(json);
public class JsonObject
public Data data get; set;
public class Data
public List<string> count get; set;
public List<Doc> list get; set;
public class Doc
public string title get; set;
public int rotate get; set;
public string sort_key get; set;
public string tag_ids get; set;
public string doc_id get; set;
public string co_token get; set;
public string p get; set;
public string t get; set;
public string c get; set;
public string updated get; set;
【讨论】:
以上是关于使用 c# 递归解析 JSON的主要内容,如果未能解决你的问题,请参考以下文章