So I am trying to deserialize some data into a collection. I am getting an issue when I try to deserialize the contents of a file that contains the json.
This is how I store the data.
WebItem = new WebsiteItemViewModel
{
Title = "Twitter Item",
Description = "Here is a long description that might be crossing the bounds but thats fine.",
Image = "../resources/twitter.png"
};
string resultJson2 = JsonConvert.SerializeObject(WebItem);
File.AppendAllText("Testfile", resultJson2);
And this is how I try to deserialzie the data :
private void OnLoadData_Click(object sender, RoutedEventArgs e)
{
string data = File.ReadAllText("Testfile");
var deserialize = JsonConvert.DeserializeObject<WebsiteItemViewModel>(data);
}
But when I click to deserialize it throws that error
Newtonsoft.Json.JsonReaderException: 'Additional text encountered after finished reading JSON content: {. Path '', line 1, position 154.'
And this is what the json data looks like.
{"Title":"Facebook Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/Facebook.png"}{"Title":"Facebook Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/Facebook.png"}{"Title":"Twitter Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/twitter.png"}{"Title":"Facebook Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/Facebook.png"}{"Title":"Twitter Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/twitter.png"}{"Title":"Twitter Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/twitter.png"}{"Title":"Facebook Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/Facebook.png"}{"Title":"Facebook Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/Facebook.png"}{"Title":"Twitter Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/twitter.png"}{"Title":"Facebook Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/Facebook.png"}{"Title":"Twitter Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/twitter.png"}{"Title":"Twitter Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/twitter.png"}{"Title":"Facebook Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/Facebook.png"}{"Title":"Facebook Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/Facebook.png"}{"Title":"Facebook Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/Facebook.png"}{"Title":"Twitter Item","Description":"Here is a long description that might be crossing the bounds but thats fine.","Image":"../resources/twitter.png"}
Model
[Serializable]
public class ClassName...
[JsonProperty("Title")]
public string Title
{
get { return _title; }
set
{
_title = value;
OnPropertyChanged("Title");
}
}
[JsonProperty("Description")]
public string Description
{
get { return _description; }
set
{
_description = value;
OnPropertyChanged("Description");
}
}
[JsonProperty("Image")]
public string Image
{
get { return _image; }
set
{
_image = value;
OnPropertyChanged("Image");
}
}
I would deserializeObject Json data to be List<WebsiteItemViewModel> collection object by a method.
private List<WebsiteItemViewModel> readData()
{
string data = File.ReadAllText("Testfile");
return JsonConvert.DeserializeObject<List<WebsiteItemViewModel>>(data) ?? new List<WebsiteItemViewModel>();
}
then you can append your WebsiteItemViewModel type WebItem new object in the collection the use WriteAllText to overwrite the file to make sure the JSON data is the newest.
var list = readData();
WebsiteItemViewModel WebItem = new WebsiteItemViewModel
{
Title = "Twitter Item",
Description = "Here is a long description that might be crossing the bounds but thats fine.",
Image = "../resources/twitter.png"
};
list.Add(WebItem);
string resultJson2 = JsonConvert.SerializeObject(list);
File.WriteAllText("Testfile", resultJson2);
NOTE
use WriteAllText instead of AppendAllText because AppendAllText will append the JSON data on the end.
WriteAllText will overwrite the file to keep your JSON be a valid array string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With