C
C#2y ago
danimasona

JSON C#

Can someone help with this code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Newtonsoft.Json; using System.IO; namespace menu_proba { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public class Feedback { [JsonProperty("name")] public string Name { get; set; } [JsonProperty("email")] public string Email { get; set; } [JsonProperty("message")] public string Message { get; set; } } private void Form1_Load(object sender, EventArgs e) {
} private void button1_Click(object sender, EventArgs e) { Feedback feedback = new Feedback() { Name = textBox1.Text, Email = textBox2.Text, Message = textBox3.Text }; List<Feedback> feedbackList = new List<Feedback>(); if (File.Exists("feedback.json")) { string jsonText = File.ReadAllText("feedback.json"); var feedbackObj = JsonConvert.DeserializeObject<Feedback>(jsonText.Trim()); feedbackList.Add(feedbackObj); } feedbackList.Add(feedback); string json = JsonConvert.SerializeObject(feedbackList.ToArray()); File.WriteAllText("feedback.json", json); dataGridView1.DataSource = feedbackList; } } } the problem is in this line: var feedbackObj = JsonConvert.DeserializeObject<Feedback>(jsonText.Trim());
12 Replies
Relevant
Relevant2y ago
I think you answered your own question If you already know the line that is the problem What's the error?
danimasona
danimasonaOP2y ago
Newtonsoft.Json.JsonReaderException: 'Additional text encountered after finished reading JSON content: {. Path '', line 1, position 58.'
Relevant
Relevant2y ago
have you looked at the jsonText value to see if it's invalid json?
danimasona
danimasonaOP2y ago
yes, I think everything is good: { "Name": "John", "Email": "[email protected]", "Message": "This is a feedback message." }, { "Name": "Mary", "Email": "[email protected]", "Message": "This is another feedback message." } ]
Angius
Angius2y ago
Seems [ is missing at the start
danimasona
danimasonaOP2y ago
[ { "Name": "John", "Email": "[email protected]", "Message": "This is a feedback message." }, { "Name": "Mary", "Email": "[email protected]", "Message": "This is another feedback message." } ]
Angius
Angius2y ago
And you're trying to deserialize to Feedback Singular This is an array As denoted by square braces And, well, the existence of more than one objects
danimasona
danimasonaOP2y ago
do you have any idea how to fix it
Relevant
Relevant2y ago
var feedbackObjs = JsonConvert.DeserializeObject<Feedback[]>(jsonText.Trim());
danimasona
danimasonaOP2y ago
now there is a problem here: feedbackList.Add(feedbackObj);
Relevant
Relevant2y ago
Yes, because feedbackObj is not a Feedback It's a Feedback[] Try feedbackList.AddRange(feedbackObjs) That is if a list can take an array, not sure off the top of my head if it can If not, just change your deserialize to parse to a list instead of an array
danimasona
danimasonaOP2y ago
thanks
Want results from more Discord servers?
Add your server