❔ 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
Daar Archival (Autocorrect)
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.
Daar Archival (Autocorrect)
how would i deserialize it with Newtonsoft?
Thinker
Thinker2y ago
Any particular reason you're using Newtonsoft?
Daar Archival (Autocorrect)
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.
Daar Archival (Autocorrect)
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?
Daar Archival (Autocorrect)
the latest one, idk how to view it tho
Thinker
Thinker2y ago
Open your csproj file
Daar Archival (Autocorrect)
uhh where's that, in \repos? ok opened what next?
Thinker
Thinker2y ago
What is the TargetFramework?
Daar Archival (Autocorrect)
uh what wheres that
Thinker
Thinker2y ago
in the csproj Or like, show the entire file
Daar Archival (Autocorrect)
i dont get what you mean
Daar Archival (Autocorrect)
i opened it tho and its just my solution
Thinker
Thinker2y ago
Open it in a text editor
Daar Archival (Autocorrect)
so vsc? <TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion>
Thinker
Thinker2y ago
ah, you're on Framework which is not the latest version
Daar Archival (Autocorrect)
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
Daar Archival (Autocorrect)
im going to use regular forms
Daar Archival (Autocorrect)
.NET 6.0 or .NET 7.0?
Thinker
Thinker2y ago
7 is the latest
Daar Archival (Autocorrect)
okay, should i copy the code form my old project
Thinker
Thinker2y ago
I think that will work? probably
Daar Archival (Autocorrect)
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.
Daar Archival (Autocorrect)
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
Daar Archival (Autocorrect)
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
Daar Archival (Autocorrect)
Hmm, it says an object reference is required now?
Thinker
Thinker2y ago
show your code
Daar Archival (Autocorrect)
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
Daar Archival (Autocorrect)
in the example class its Updated
Thinker
Thinker2y ago
Example is a type, and Updated is not static so Example.Updated is not valid
Daar Archival (Autocorrect)
oh ok what should i do instead
Thinker
Thinker2y ago
You have Example? Updated = JsonSerializer ...;, so you should do Updated.Updated
Daar Archival (Autocorrect)
Oh okay
Thinker
Thinker2y ago
Or you know, rename the variable to example
Daar Archival (Autocorrect)
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.'
Daar Archival (Autocorrect)
how to fix?
Thinker
Thinker2y ago
Show your class
Thinker
Thinker2y ago
And what does your JSON string look like?
Daar Archival (Autocorrect)
[{"Example":{"updated":false,},] shortened
Thinker
Thinker2y ago
That's an array first of all
Daar Archival (Autocorrect)
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
Daar Archival (Autocorrect)
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
Daar Archival (Autocorrect)
so i just need to do Example[updated]?
Thinker
Thinker2y ago
But the objects in the array have a property called Example
Daar Archival (Autocorrect)
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;
Daar Archival (Autocorrect)
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
Daar Archival (Autocorrect)
this im pretty sure
Thinker
Thinker2y ago
Odd that it says something about an invalid < character in that case.
Daar Archival (Autocorrect)
yeah idk what the < error is
Thinker
Thinker2y ago
Try debugging your project and looking at what the data actually is
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.

Did you find this page helpful?