C
C#3y ago
DayKnight

WPF read and write to Json [Answered]

The title says it all, I don't know how to read and write to Json properly. I tried some things but they don't seem to work
static List<persondata> persondata()
{
string fileName = @"C:\Users\elko\source\repos\JsonData\JsonData\bin\Debug\net6.0-windows\Persons.json";
if (File.Exists(fileName))
{
var persons = JsonConvert.DeserializeObject<List<persondata>>
(File.ReadAllText(fileName));

return persons;
}
return null;
}
}
static List<persondata> persondata()
{
string fileName = @"C:\Users\elko\source\repos\JsonData\JsonData\bin\Debug\net6.0-windows\Persons.json";
if (File.Exists(fileName))
{
var persons = JsonConvert.DeserializeObject<List<persondata>>
(File.ReadAllText(fileName));

return persons;
}
return null;
}
}
i have looked for tutorials guides everywhere, but nothing worked for me. I must be doing something wrong
30 Replies
ero
ero3y ago
what does the json look like, and what does your persondata class look like
DayKnight
DayKnightOP3y ago
My class is just this
public class persondata
{
public string name { get; set; }
public string address { get; set; }
public string city { get; set; }
}
public class persondata
{
public string name { get; set; }
public string address { get; set; }
public string city { get; set; }
}
My Json is empty right now
ero
ero3y ago
i mean if the json is empty, what data do you expect to deserialize into the list?
DayKnight
DayKnightOP3y ago
<a:aPES4_WeirdStare:827518462745772084> Whops I forgot, that I actually had to have something in my Json. Thanks xd
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega3y ago
Be wary of mixing newtonsoft with System.Text.Json thou
ero
ero3y ago
can nsj/stj work with init props?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
ero
ero3y ago
also your code won't work will it
public record PersonData(
[JsonPropertyName("name")] string Name,
[JsonPropertyName("address")] string Address,
[JsonPropertyName("city")] string City
);
public record PersonData(
[JsonPropertyName("name")] string Name,
[JsonPropertyName("address")] string Address,
[JsonPropertyName("city")] string City
);
this is probably what you meant
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
DayKnight
DayKnightOP3y ago
Wow thanks everyone, I had no idea about the PascalCase and camelCase
TheRanger
TheRanger3y ago
JsonConvert.DeserializeObject is from Newtonsoft Package so use [JsonProperty] instead
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
TheRanger
TheRanger3y ago
newtonsoft isnt case sensitive i think
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
ero
ero3y ago
stj has JsonConvert?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
ero
ero3y ago
i've only ever used JsonSerializer
TheRanger
TheRanger3y ago
No the attribute JsonPropertyName is totally useless there
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
TheRanger
TheRanger3y ago
and this JsonSerializer.Deserialize<
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3y ago
tebeco#0205
REPL Result: Success
using System.Text.Json;
using System.Text.Json.Serialization;

public record PersonData(
[property: JsonPropertyName("name")] string Name,
[property: JsonPropertyName("address")] string Address,
[property: JsonPropertyName("city")] string City
);

var json = """
[
{
"name": "x1",
"address": "y1",
"city": "z1"
},
{
"name": "x2",
"address": "y2",
"city": "z2"
}
]
""";

var people = System.Text.Json.JsonSerializer.Deserialize<List<PersonData>>(json);

(people[0], people[1])
using System.Text.Json;
using System.Text.Json.Serialization;

public record PersonData(
[property: JsonPropertyName("name")] string Name,
[property: JsonPropertyName("address")] string Address,
[property: JsonPropertyName("city")] string City
);

var json = """
[
{
"name": "x1",
"address": "y1",
"city": "z1"
},
{
"name": "x2",
"address": "y2",
"city": "z2"
}
]
""";

var people = System.Text.Json.JsonSerializer.Deserialize<List<PersonData>>(json);

(people[0], people[1])
Result: ValueTuple<PersonData, PersonData>
{
"item1": {
"name": "x1",
"address": "y1",
"city": "z1"
},
"item2": {
"name": "x2",
"address": "y2",
"city": "z2"
}
}
{
"item1": {
"name": "x1",
"address": "y1",
"city": "z1"
},
"item2": {
"name": "x2",
"address": "y2",
"city": "z2"
}
}
Compile: 639.697ms | Execution: 93.213ms | React with ❌ to remove this embed.
TheRanger
TheRanger3y ago
iirc, stj is case sensitive, but u can pass an option settings to make it case insensitive
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
DayKnight
DayKnightOP3y ago
Wow thanks everyone, I appreciate it a lot! It actually works. I can write to my Json. I don't know if it reads it, but I can suddenly write to it, I have not checked if it reads yet. Thanks! 👍 cathi
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
DayKnight
DayKnightOP3y ago
Ah okay, that's fine. I don't have a preference, as long as I learn a way to read and write to Json. Doesn't matter which way to me. I appreciate all the help catlaugh
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server