C
C#•3mo ago
Core Dream

JSON Parsing to TreeView

Hello. I'm still banging my head on my desk the past 2 days trying to read a .json file which has a "table" root, into a TreeView. You'd think it would be easy for a dumbass but nope. Please help 😦 I tried ChatGPT, I tried Google, MS Learn, etc and I am a loser somehow. The example CharacterMapping.json file:
{
"table": [
{
"id": 100,
"character_path": 9155864901130912278,
"attire_path": 5838515536658117378,
"basemodel_mdl_path": 9608790949800725399,
"basemodel_mtls_path": 16869767090390095004,
"attire_mdl_path": 5619533544245612033,
"attire_mtls_path": 3740595121255541638,
"unk2": 30
},
{
"id": 101,
"character_path": 12279268510363823451,
"attire_path": 16519486993216130268,
"basemodel_mdl_path": 5133769854344942046,
"basemodel_mtls_path": 8091982216995671175,
"attire_mdl_path": 14803390407684558538,
"attire_mtls_path": 239947156153590923,
"unk2": 28
},
{
"id": 101,
"attire_num": 1,
"character_path": 12279268510363823451,
"attire_path": 15744211445203291278,
"basemodel_mdl_path": 5133769854344942046,
"basemodel_mtls_path": 8091982216995671175,
"attire_mdl_path": 8186642687843084600,
"attire_mtls_path": 9716164581340245505,
"unk2": 28
}
]
}
{
"table": [
{
"id": 100,
"character_path": 9155864901130912278,
"attire_path": 5838515536658117378,
"basemodel_mdl_path": 9608790949800725399,
"basemodel_mtls_path": 16869767090390095004,
"attire_mdl_path": 5619533544245612033,
"attire_mtls_path": 3740595121255541638,
"unk2": 30
},
{
"id": 101,
"character_path": 12279268510363823451,
"attire_path": 16519486993216130268,
"basemodel_mdl_path": 5133769854344942046,
"basemodel_mtls_path": 8091982216995671175,
"attire_mdl_path": 14803390407684558538,
"attire_mtls_path": 239947156153590923,
"unk2": 28
},
{
"id": 101,
"attire_num": 1,
"character_path": 12279268510363823451,
"attire_path": 15744211445203291278,
"basemodel_mdl_path": 5133769854344942046,
"basemodel_mtls_path": 8091982216995671175,
"attire_mdl_path": 8186642687843084600,
"attire_mtls_path": 9716164581340245505,
"unk2": 28
}
]
}
The source is here (FUCK GITHUB it keeps breaking)
32 Replies
Salman
Salman•3mo ago
Okay so what exactly is the problem with this Json and how it relates to GitHub
Core Dream
Core Dream•3mo ago
Ok, #1 skip github please read the post I just need an answer about it 😦 code is in the .zip, you cna keep it, its not copyrighted its just a winform with 1 class and 1 model
Salman
Salman•3mo ago
I've read the post . You have not described much. There's a table array in the Json and each object has a few properties. So what's the problem in it
Core Dream
Core Dream•3mo ago
I want them in a treeview so like this:
100
> Attire Number
> Character Folder Path
...
101
> Attire Number
> Character Folder Path
...
102
> Attire Number
> Character Folder Path
...
103
> Attire Number
> Character Folder Path
...
...
100
> Attire Number
> Character Folder Path
...
101
> Attire Number
> Character Folder Path
...
102
> Attire Number
> Character Folder Path
...
103
> Attire Number
> Character Folder Path
...
...
Game modding sucks and the 1 community I was in, is a toxic dump so so trying to make things myself
SleepWellPupper
SleepWellPupper•3mo ago
Also, posting a zip or even txt file of your code is frowned upon. Use $paste instead please.
MODiX
MODiX•3mo ago
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Core Dream
Core Dream•3mo ago
hold pls
Salman
Salman•3mo ago
And where do you want to display that information in this structure?
Core Dream
Core Dream•3mo ago
https://paste.mod.gg/ypsrxkvwbqzi/0 JsonParser class (diff namespace)
BlazeBin - ypsrxkvwbqzi
A tool for sharing your source code with the world!
Salman
Salman•3mo ago
In your WinForms app ?
Core Dream
Core Dream•3mo ago
using GameModderStudioNonMVVM.Helpers;

namespace GameModderStudioNonMVVM.Presentation;

public partial class MainApp : Form
{
public MainApp()
{
InitializeComponent();
}

private void GetCharacterMappingIDs()
{
JsonParser.ReadToTreeView(@"./CharacterMappingSample.json");
}

private void ReadJSON_Click(object sender, EventArgs e)
{
GetCharacterMappingIDs();
}
}
using GameModderStudioNonMVVM.Helpers;

namespace GameModderStudioNonMVVM.Presentation;

public partial class MainApp : Form
{
public MainApp()
{
InitializeComponent();
}

private void GetCharacterMappingIDs()
{
JsonParser.ReadToTreeView(@"./CharacterMappingSample.json");
}

private void ReadJSON_Click(object sender, EventArgs e)
{
GetCharacterMappingIDs();
}
}
MainApp form code behind Yes, in the TreeView hell, a listbox if i need to if its not possible lol The issue probably isn't even the TreeView, it's my parsing I always did suck at structures in json
Salman
Salman•3mo ago
Well I'd just fetch and deserialize the Json to a List in the code behind and would show them in the form of the code behind and would design accordingly or would use an existing control
Core Dream
Core Dream•3mo ago
(Why aren't you using WPF? Because this is TESTING, not gonna code 500 lines oof xaml to put a button and treeview on a form) I'm the designer too so i guess ill do it its just me, no team and no job
using GameModderStudioNonMVVM.Model;
using System.Text.Json;

namespace GameModderStudioNonMVVM.Helpers;

internal class JsonParser
{
public static List<CharacterMapping> ReadToTreeView(string fileName)
{
// Read entire JSON into a variable
var jsonString = File.ReadAllText(fileName);

// Deserialize the data and put it into the model.
var characterMapping = JsonSerializer.Deserialize<RootObject>(jsonString);

foreach (var item in characterMapping.Table)
{
TreeNode node = new TreeNode(item.Id.ToString());
foreach (var prop in typeof(RootObject).GetProperties())
{
if (!prop.Name.StartsWith("unk") && !prop.Name.StartsWith("attire_"))
node.Nodes.Add(new TreeNode($"{prop.Name}: {prop.GetValue(item)}"));

}
}
}
}
using GameModderStudioNonMVVM.Model;
using System.Text.Json;

namespace GameModderStudioNonMVVM.Helpers;

internal class JsonParser
{
public static List<CharacterMapping> ReadToTreeView(string fileName)
{
// Read entire JSON into a variable
var jsonString = File.ReadAllText(fileName);

// Deserialize the data and put it into the model.
var characterMapping = JsonSerializer.Deserialize<RootObject>(jsonString);

foreach (var item in characterMapping.Table)
{
TreeNode node = new TreeNode(item.Id.ToString());
foreach (var prop in typeof(RootObject).GetProperties())
{
if (!prop.Name.StartsWith("unk") && !prop.Name.StartsWith("attire_"))
node.Nodes.Add(new TreeNode($"{prop.Name}: {prop.GetValue(item)}"));

}
}
}
}
Salman
Salman•3mo ago
Try this
Core Dream
Core Dream•3mo ago
i modified it to include List<modenamehere> but what am i returning?
Salman
Salman•3mo ago
Lemme eat lunch first
Core Dream
Core Dream•3mo ago
I wish I had my old code from when I did a inventory app, same idea for models back 10 years ago :/ np take your time , i appreciate the help. its 5am for me lol
Salman
Salman•3mo ago
@/dev/null/CoreDream So listen. The algorithm that I propose is as follows: 1. First of all in your code behind get all the Json in a string from your Json file. 2. Now deserialize the obtained json to a List of the object. Where object should be extractly structured like the objects in the Json. Like same properties etc. Like create a model class. 3. Once you have successfully deserialized the Json to the list now you have the data you need. So you can look for some TreeView controls or any control tha can help you design it the way you want, so you would just bind the obtained data to those controls. For the step 3 , you can seek further expertise in #gui
Core Dream
Core Dream•3mo ago
ty
Salman
Salman•3mo ago
Also I've just checked that there already exists a TreeView control in the WinForms. So after deserializing The data you just need to bind the data with that control and you are done
Core Dream
Core Dream•3mo ago
no problem
cap5lut
cap5lut•3mo ago
JsonSerializer.Deserialize can also take a stream btw, so u could simply open a file stream and pass that, especially for a big file this will require a lot less memory
leowest
leowest•3mo ago
if only it was that easy in winforms, sadly u have to make your own method to populate teh treeview
Salman
Salman•3mo ago
oh! I looked at some examples but Idk how binding works (yk am a webview guy)
leowest
leowest•3mo ago
you suggested him now u figure it out! :catpout:
Salman
Salman•3mo ago
:pepeawooga:
Core Dream
Core Dream•3mo ago
ty yeah fuck winforms, i think wpf is better afterall XD
leowest
leowest•3mo ago
if ur going wpf then do take the time to learn xaml its important and will help u along your journey how the controls work, templates, bindings etc
Salman
Salman•3mo ago
for the simple use case like that I won't do such migration, as apparently it's kind of a modding program
leowest
leowest•3mo ago
I mean its up to him to decide what he wants to use. if he feels winforms is not it... then that's that... Winfroms can be easy to get up and going but mvvm with winform is not really that simple as it is with XAML based frameworks because not all controls in winforms allow for binding data like datagrid or listbox etc or accept a datasource
Salman
Salman•3mo ago
alright, the last part is bad. The way it's popular it should've supported proper binding at least . Not sure what else they do
leowest
leowest•3mo ago
it does in some controls not everything and it will be improved on .net 9
Want results from more Discord servers?
Add your server