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());
} 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());
23 replies