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.
CodePudding user response:
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; }
}
CodePudding user response:
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; }
}