❔ JSON array to List KeyValuePair with Newtonsoft.JSON?
As the title reads, i'm attempting to load a config file (which successfully works) except I am struggling to figure out how I can take in my image assets as a KeyValuePair with the JSON name and value being the parameters.
Here's my JSON code snippet:
Here's my C# translation using Newtonsoft.Json:
How would I correctly do this?
Thanks.
44 Replies
Convert JSON to C# Classes Online - Json2CSharp Toolkit
Convert any JSON object to C# classes online. Json2CSharp is a free toolkit that will help you generate C# classes on the fly.
here's what it generated from ur current json code snippet
you can change the property name to
Image
and put the attribute [JsonProperty("image_asset_urls")]
above the propertyhttps://discord.com/channels/143867839282020352/1042207799440060497/1042211981320519751
mtreit just showed in a different post that there was a such feature in VS/Rider which automatically parses JSON to C# objects, so you don't have to use https://json2csharp.com/ 🙂
mtreit#6470
Quoted by
<@!228574670503280640> from #how to deserialize JSON array (click here)
React with ❌ to remove this embed.
Thanks for the response - i'm struggling to implement this. How would I go about doing this while also ensuring no JSON data is hard coded (i'd like to add to the list in the future without code changes)
I've implemented to this stage - any assistance is appreciated
is this ur whole config.json or is there more than that?
There is more, one moment
not asking for this
Would you want a sample JSON?
Sorry, was getting there
as long as
"image_asset_urls"
is inside the root of the json, it will workok so what exactly isnt working?
How would I call for both the name of the json value (Ex: bot_icon) and the url?
I should be able to call for the name value aswell
Considering they will be dynamic
you want to put them into a dictionary?
yeah correct, originally I tried a keyvaluepair above
but had no luck
apologies for my poor explanation
never tried keyvaluepair but dictionary works
after all, Dictionary is a collection of KeyValuePair
ah wait
Awesome, assuming i'd place above the Dictionary line also?
roger
its an array, let me fix it
👌
Appreciate it
code edited, not sure if it will work
Will give it a shot, thank you
works fine on my end
One more for you, what would the best way to call this be? Previously I used LINQ, unsure what changes would be required here
Thanks a bunch for this
Very appreciative
what does the error message say?
i have no idea what the type of
i
and Keys
areNeed to figure out how to filter the keys in the collection to find the correct key, then pull the value ideally
Essentially looking to pull any URL from the JSON data using the key value associated with it
ah i think i see what ur trying to do
ur trying to get the value of "bot_icon" ?
Sweet, this way I can keep all images external for easy updates.
Correct, and if the value is found I want the url associated with bot_icon
what if you have multiple
bot_icon
s in your List ?I don't see it happening. Each entry in the JSON list would be unique
will it always be the first element in the list?
No
It would be changed in position often
well this is one way of doing it
but there is probably a better way i have to think/remember of
but i would suggest to make a method in the Root class to return it
cleaner, and avoid repeating code in different places
Thanks, i'll implement this ASAP
Working!! Thanks a tone. Will leave this open incase you or someone else stumbles across a better way of doing this.
i already did, 1 sec
You're a machine 😛
then u just do
IconUrl = config.GetValue("bot_icon");
Similar to what I had in mind, great stuff
Thanks a lot my friend
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.