C
C#2y ago
Zarchical

❔ How do I get item values from an array?

Basically, this is the array i have stored in a var: [{"Example":{"updated":false,},] (i shortened it to make it more readable) I want to get Example.updated and return it, how would i do that?
74 Replies
nboyz98
nboyz982y ago
Assuming it's not json you could do x[0].Updated where x is the name of your var
Zarchical
ZarchicalOP2y ago
so x is my var, 0 is Example and updated is updated?
Thinker
Thinker2y ago
If this is a string then you'll have to deserialize it to a class then access that.
Zarchical
ZarchicalOP2y ago
how would i deserialize it with Newtonsoft?
Thinker
Thinker2y ago
Any particular reason you're using Newtonsoft?
Zarchical
ZarchicalOP2y ago
It's the only way I could find, should I use something else then?
MODiX
MODiX2y ago
thinker227#5176
REPL Result: Success
record Root(Body Example);
record Body(bool Updated);

string json = """
[
{
"Example": {
"Updated": true
}
}
]
""";
Root[] root = System.Text.Json.JsonSerializer.Deserialize<Root[]>(json);
Console.WriteLine(root[0].Example.Updated);
record Root(Body Example);
record Body(bool Updated);

string json = """
[
{
"Example": {
"Updated": true
}
}
]
""";
Root[] root = System.Text.Json.JsonSerializer.Deserialize<Root[]>(json);
Console.WriteLine(root[0].Example.Updated);
Console Output
True
True
Compile: 667.971ms | Execution: 134.660ms | React with ❌ to remove this embed.
Thinker
Thinker2y ago
System.Text.Json is a JSON library built into the standard library with about the same capabilities as Newtonsoft.
Zarchical
ZarchicalOP2y ago
Alright It says it doesn't exist It's error CS0234 Also I'm using Windows Forms So it happens when the form is loaded and on button click (currently only doing it on button click)
Thinker
Thinker2y ago
What .NET version are you using?
Zarchical
ZarchicalOP2y ago
the latest one, idk how to view it tho
Thinker
Thinker2y ago
Open your csproj file
Zarchical
ZarchicalOP2y ago
uhh where's that, in \repos? ok opened what next?
Thinker
Thinker2y ago
What is the TargetFramework?
Zarchical
ZarchicalOP2y ago
uh what wheres that
Thinker
Thinker2y ago
in the csproj Or like, show the entire file
Zarchical
ZarchicalOP2y ago
i dont get what you mean
Zarchical
ZarchicalOP2y ago
i opened it tho and its just my solution
Thinker
Thinker2y ago
Open it in a text editor
Zarchical
ZarchicalOP2y ago
so vsc? <TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion>
Thinker
Thinker2y ago
ah, you're on Framework which is not the latest version
Zarchical
ZarchicalOP2y ago
k, should i just make a regular forms app?
Thinker
Thinker2y ago
$newproject
MODiX
MODiX2y ago
When creating a new project, prefer using .NET over .NET Framework, unless you have a very specific reason to be using .NET Framework. .NET Framework is now legacy code and only get security fix updates, it no longer gets new features and is not recommended. https://cdn.discordapp.com/attachments/569261465463160900/899381236617855016/unknown.png
Thinker
Thinker2y ago
You should practically never use Framework
Zarchical
ZarchicalOP2y ago
kk
Zarchical
ZarchicalOP2y ago
im going to use regular forms
Zarchical
ZarchicalOP2y ago
.NET 6.0 or .NET 7.0?
Thinker
Thinker2y ago
7 is the latest
Zarchical
ZarchicalOP2y ago
okay, should i copy the code form my old project
Thinker
Thinker2y ago
I think that will work? probably
Zarchical
ZarchicalOP2y ago
okay that worked now how would i deserialize it with System.Text.Json? oh god error: error CS0103: The name 'Form1_Load' does not exist in the current context i know why but idk how to fix ok fixed how to deserialize?
Thinker
Thinker2y ago
How to serialize and deserialize JSON using C# - .NET
Learn how to use the System.Text.Json namespace to serialize to and deserialize from JSON in .NET. Includes sample code.
Zarchical
ZarchicalOP2y ago
in the example with WeatherForecast, should i put it outside of the button hmm im confused on this @thinker227 most of the code works, but Console.WriteLine($"Example Updated: {Example.Updated}"); says that Example doesn't exist in the current context How do I fix that?
Thinker
Thinker2y ago
What does you class look like? The issue is that Example doesn't contain a property named Updated
Zarchical
ZarchicalOP2y ago
public class Example {
public bool Updated { get; set; }
public string? Version { get; set; }
}
public class Example {
public bool Updated { get; set; }
public string? Version { get; set; }
}
Thinker
Thinker2y ago
Looks fine
Zarchical
ZarchicalOP2y ago
Hmm, it says an object reference is required now?
Thinker
Thinker2y ago
show your code
Zarchical
ZarchicalOP2y ago
i used an api to get the json btw, thats why its different, but its definitely json as on the api docs it says it returns json
Thinker
Thinker2y ago
Well your variable is called Updated for some reason Not Example
Zarchical
ZarchicalOP2y ago
in the example class its Updated
Thinker
Thinker2y ago
Example is a type, and Updated is not static so Example.Updated is not valid
Zarchical
ZarchicalOP2y ago
oh ok what should i do instead
Thinker
Thinker2y ago
You have Example? Updated = JsonSerializer ...;, so you should do Updated.Updated
Zarchical
ZarchicalOP2y ago
Oh okay
Thinker
Thinker2y ago
Or you know, rename the variable to example
Zarchical
ZarchicalOP2y ago
I'm just trying to see if it works So if it's an updated bool and its Updated.Updated will it return the value of Updated in Example class? or would i have to do Updated.Value
- System.Text.Json.JsonException: 'The JSON value could not be converted to WindowsForm4.Form1+Example. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'
- System.Text.Json.JsonException: 'The JSON value could not be converted to WindowsForm4.Form1+Example. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'
Zarchical
ZarchicalOP2y ago
Zarchical
ZarchicalOP2y ago
how to fix?
Thinker
Thinker2y ago
Show your class
Zarchical
ZarchicalOP2y ago
Thinker
Thinker2y ago
And what does your JSON string look like?
Zarchical
ZarchicalOP2y ago
[{"Example":{"updated":false,},] shortened
Thinker
Thinker2y ago
That's an array first of all
Zarchical
ZarchicalOP2y ago
oh then why did it say it returned json eh prob doc mistake
Thinker
Thinker2y ago
that is json but the root of the json is an array
Zarchical
ZarchicalOP2y ago
ah ok so how to deserialize array then
Thinker
Thinker2y ago
It's an array containing objects containing an object called Example containing a boolean called updated Example[] probably
Zarchical
ZarchicalOP2y ago
so i just need to do Example[updated]?
Thinker
Thinker2y ago
But the objects in the array have a property called Example
Zarchical
ZarchicalOP2y ago
Yeah ok so say the class is called Example1 and the array is [{"Example2":{"updated":false,},] what do i do to get updated from Example2? and then change a textBox's text? (i know how to change text, just don't know the input required)
Thinker
Thinker2y ago
If your json is
[
{
"Example": {
"Updated": false
}
}
]
[
{
"Example": {
"Updated": false
}
}
]
then your class structure should be
class Root
{
public Example Example { get; set; }
}

class Example
{
public bool Updated { get; set; }
}
class Root
{
public Example Example { get; set; }
}

class Example
{
public bool Updated { get; set; }
}
and you should be deserializing the string using
Root[] array = JsonSerializer.Deserialize<Root[]>(json);
bool updated = array[0].Example.Updated;
Root[] array = JsonSerializer.Deserialize<Root[]>(json);
bool updated = array[0].Example.Updated;
Zarchical
ZarchicalOP2y ago
i'll try that out in a mo @thinker227 error: System.Text.Json.JsonException: ''<' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.' on the line Root[] array = JsonSerializer.Deserialize<Root[]>(json);
Thinker
Thinker2y ago
What does you json look like
Zarchical
ZarchicalOP2y ago
this im pretty sure
Thinker
Thinker2y ago
Odd that it says something about an invalid < character in that case.
Zarchical
ZarchicalOP2y ago
yeah idk what the < error is
Thinker
Thinker2y ago
Try debugging your project and looking at what the data actually is
Zarchical
ZarchicalOP2y ago
what
Thinker
Thinker2y ago
$debug
MODiX
MODiX2y ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Thinker
Thinker2y ago
You're getting the error because your json is weird
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server