简体   繁体   English

用c#递归解析JSON

[英]Recursive parse JSON with c#

I try to do a recursive parse but it doesn't work, it only parse the first title.我尝试进行递归解析但它不起作用,它只解析第一个标题。

I already tried that: C# parse recursive json but it didn't work.我已经试过了: C# parse recursive json但它没有用。

I also tried a parse with Regex but it didn't work either, it only captures the first title.我也尝试过使用 Regex 进行解析,但它也不起作用,它只捕获了第一个标题。

So I'm hoping you can help me solve the problem that's been blocking my progress :/所以我希望你能帮助我解决阻碍我进步的问题:/

My Json我的杰森

{ 
   "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"
         }
      ],
   }
}
  1. Parse the string into an object that c# can use to iterate through.将字符串解析为 c# 可用于迭代的对象。
  2. then use the foreach loop to access the items of the list.然后使用 foreach 循环访问列表中的项目。
  3. Add title to a stringbuilder为字符串生成器添加标题
  4. Save the collected titles to a file.将收集的标题保存到文件中。
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());

I used Newtonsoft.Json package to convert json from file data.json which contains the json data我使用 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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM