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());
} 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
I think you answered your own question
If you already know the line that is the problem
What's the error?
Newtonsoft.Json.JsonReaderException: 'Additional text encountered after finished reading JSON content: {. Path '', line 1, position 58.'
have you looked at the jsonText value to see if it's invalid json?
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."
}
]
Seems
[
is missing at the start[
{
"Name": "John",
"Email": "[email protected]",
"Message": "This is a feedback message."
},
{
"Name": "Mary",
"Email": "[email protected]",
"Message": "This is another feedback message."
}
]
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 objectsdo you have any idea how to fix it
var feedbackObjs = JsonConvert.DeserializeObject<Feedback[]>(jsonText.Trim());
now there is a problem here: feedbackList.Add(feedbackObj);
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 arraythanks