C
C#2y ago
DayKnight

Can't read Json in WPF

I don't think I understand this error. It requires an array? but how? Should I make a loop?
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;
}
}
8 Replies
DayKnight
DayKnight2y ago
So basically, The program runs just fine when Json is empty. But as soon there is something inside the Json it fails, because it isn't an array? I got some help yesterday so I could write to my Json. I can't read it though
ero
ero2y ago
show the json
DayKnight
DayKnight2y ago
{"Name":"awrfawf","Address":"awgfaeg","City":"aegae"}
{"Name":"awrfawf","Address":"awgfaeg","City":"aegae"}
I tried that, it worked with Javascript, but c# removes it when I write to Json
File.WriteAllText(@"C:\Users\elko\source\repos\JsonData\JsonData\bin\Debug\net6.0-windows\Persons.json", JsonConvert.SerializeObject(persondata));

using (StreamWriter file = File.CreateText(@"C:\Users\elko\source\repos\JsonData\JsonData\bin\Debug\net6.0-windows\Persons.json"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, persondata);
}
File.WriteAllText(@"C:\Users\elko\source\repos\JsonData\JsonData\bin\Debug\net6.0-windows\Persons.json", JsonConvert.SerializeObject(persondata));

using (StreamWriter file = File.CreateText(@"C:\Users\elko\source\repos\JsonData\JsonData\bin\Debug\net6.0-windows\Persons.json"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, persondata);
}
It's just above
Persondata persondata = new Persondata();
{
persondata.Name = name;
persondata.Address = address;
persondata.City = city;
};
Persondata persondata = new Persondata();
{
persondata.Name = name;
persondata.Address = address;
persondata.City = city;
};
ero
ero2y ago
and show the persondata class
DayKnight
DayKnight2y ago
The person data class is this
public class Persondata
{
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("address ")]
public string Address { get; set; }

[JsonPropertyName("city")]
public string City { get; set; }

}
public class Persondata
{
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("address ")]
public string Address { get; set; }

[JsonPropertyName("city")]
public string City { get; set; }

}
Thanks hmm.. That is true, I am only making one object. But should I not be doing that, making a new object foreach click?
private void DataInputButton_Click(object sender, RoutedEventArgs e)
private void DataInputButton_Click(object sender, RoutedEventArgs e)
ero
ero2y ago
also the names in the json are in a different case
DayKnight
DayKnight2y ago
I know what you mean now. I am adding one object, and reading it as a list, which it isn't, it is not a list. So I need to find a way to write objects as a list That sounds nice, and easy. Maybe too easy... I'll try that <a:aPES_Think:493353113332219924> Like this right?
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));
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));
oh I see yeah, let me do that to the writing method too I tried something, it suddenly dosen't like it though
List<Persondata> persondata = new List<Persondata>();
{
new (persondata.Name = name);
new (persondata.Address = address);
new (persondata.City = city);
};
List<Persondata> persondata = new List<Persondata>();
{
new (persondata.Name = name);
new (persondata.Address = address);
new (persondata.City = city);
};
It says "Does not contain a definition for Name, Address, City" Hm. the error first appeared when I added List. So the name should be fine I guess? I could try to rename everything, just to get a better view Hmm true, I have tried something new
List<Persondata> persondata = new List<Persondata>();

var pers = new Persondata
{
Name = name,
Address = address,
City = city
};

string jsonSring = JsonSerializer.Serialize(pers);
List<Persondata> persondata = new List<Persondata>();

var pers = new Persondata
{
Name = name,
Address = address,
City = city
};

string jsonSring = JsonSerializer.Serialize(pers);
I have also made a constructor now, I don't know if that is correct though
ero
ero2y ago
you're only serializing that one persondata you made you never added it to the list, nor are you serializing the list
Want results from more Discord servers?
Add your server
More Posts