0

On the web side, it shows how many files and folders I add, but on the c# side, I can't get the files and folders.

{
    "name": "updater",
    "type": "folder",
    "path": "updater",
    "items": [
        {
            "name": "tas",
            "type": "folder",
            "path": "updater/tas",
            "items": [
                {
                    "name": "sdsadsd.txt",
                    "type": "file",
                    "path": "updater/tas/sdsadsd.txt",
                    "byte": 6
                },
                {
                    "name": "tass",
                    "type": "folder",
                    "path": "updater/tas/tass",
                    "items": []
                },
                {
                    "name": "trexs.txt",
                    "type": "file",
                    "path": "updater/tas/trexs.txt",
                    "byte": 14
                }
            ]
        },
        {
            "name": "tas2",
            "type": "folder",
            "path": "updater/tas2",
            "items": []
        },
        {
            "name": "tas3",
            "type": "folder",
            "path": "updater/tas3",
            "items": []
        },
        {
            "name": "tas4",
            "type": "folder",
            "path": "updater/tas4",
            "items": []
        }
    ]
}

C# Side

        public class jsonFF
        {
            public string name { get; set; }
            public string type { get; set; }
            public string path { get; set; }
            public jsonFF[] items { get; set; }
            public long byt { get; set; }
        }

        private void start_Click(object sender, EventArgs e)
        {
            try
            {
                WebClient updt = new WebClient();
                var updtJsonDownload = updt.DownloadString("http://localhost/update/updater.php");
                jsonFF[] json = JsonConvert.DeserializeObject<jsonFF[]>(updtJsonDownload.ToString());

                foreach (var item in json)
                {
                    string sss = item.name;
                    log.Text += sss + Environment.NewLine;
                }
            }
            catch (Exception ex)
            {
                log.Text = ex.ToString();
            }
        }

what I'm trying to do is I'm trying to make a tool that will automatically take files and folders as json and check if they are on the computer on the c# side or should they be downloaded and update them.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 1
    this is not going to work. your model should have not `public object items { get; set; }` but something like `public jsonFolder[] items { get; set; }`. But you might need to add `public int byt { get; set; }` to `jsonFolder` so it can take both, array of items and represent a file – T.S. Jan 10 '23 at 03:21
  • What you have is a composite pattern. And this needs a special treatment, Potencially you need to use `JObject` functionality. I found something https://stackoverflow.com/questions/70108058/how-to-deserialize-composite-list-with-custom-jsonconverter – T.S. Jan 10 '23 at 03:28
  • Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](https://creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](https://meta.stackexchange.com/q/5221) – Adriaan Jan 11 '23 at 09:04
  • Great that you managed to solve your question! Please do add it as an answer, so that others can benefit from it as well. [Answering your own question is allowed and even encouraged](https://stackoverflow.com/help/self-answer). – Adriaan Jan 11 '23 at 09:05

2 Answers2

0

your equivalent class is wrong here. I have used tool to get the equivalent class. Item class should have byt property also the Root class should have items as the list (in your case you have the object of its type)

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Item
{
    public string name { get; set; }
    public string type { get; set; }
    public string path { get; set; }
    public List<Item> items { get; set; }
    public int? byt { get; set; }
}

public class Root
{
    public string name { get; set; }
    public string type { get; set; }
    public string path { get; set; }
    public List<Item> items { get; set; }
}
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
0

You will have to fix your jsonFolder class. Replace " object items " with " List< jsonFolder> items " (or " jsonFolder[] items")

jsonFolder[] items = JsonConvert
               .DeserializeObject<jsonFolder>(updtJsonDownload.ToString()).items;

    foreach (var item in items)
    {
      string sss = item.name;
      log.Text += sss + Environment.NewLine;
     }

 //or shorter
log.Text=string.Join("\n", items.Select(i =>i.name ));

//if you need file names
log.Text = string.Join("\n", items.SelectMany(i =>i.items.Select(it =>it.name ) ))); 

//if you need  pathes
string[] pathes = items.SelectMany(i =>i.items.Select(it =>it.path)).ToArray();

public class jsonFolder
{
    public string name { get; set; }
    public string type { get; set; }
    public string path { get; set; }
    public List<jsonFolder> items { get; set; }
    public int? byt { get; set; }
}
Serge
  • 40,935
  • 4
  • 18
  • 45