-1

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.

I also tried a parse with Regex but it didn't work either, it only captures the first title.

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"
         }
      ],
   }
}
4
  • You do not have recursion. This looks like just a List. Commented Feb 15, 2020 at 15:47
  • Your json doesnt have anything that would require you to do anything recursive... JObject.Parse(json) will take care of deserialization. Also, please do explain how you intend to use this json once parsed Commented Feb 15, 2020 at 15:47
  • I want to add the titles in a text file Commented Feb 15, 2020 at 15:52
  • I don't understand how : JObject.Parse(json) can help me get the titles back. Commented Feb 15, 2020 at 15:57

2 Answers 2

2
  1. Parse the string into an object that c# can use to iterate through.
  2. then use the foreach loop to access the items of the list.
  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());

Sign up to request clarification or add additional context in comments.

2 Comments

Error on sb.ToString(); C# Argument 2: cannot convert from 'string' to 'string[]'
Use WriteAllText instead of lines.
0

I used Newtonsoft.Json package to convert json from file data.json which contains the json data

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; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.