反序列化JSON C#"类型的第一次机会异常 - newtonsoft" [英] deserializing json C# "First chance exception of type - newtonsoft"
问题描述
我试图保存和基于由论坛成员的从isolatedStorage加载文件= http://stackoverflow.com/users/1054961/shawn-kendrot\">Shawn Kendrot ,我得到了整个的这个论坛帖子。
我能保存没有问题,但然后加载deserialising我得到一个错误的JSON文件时
第一个机会异常类型的'Newtonsoft.Json.JsonSerializationException
发生在Newtonsoft.Json.DLL
我不知道我可能是做错了,因为文件被保存,它的读取正常,但不反序列化。
JSON文件有此刻的4项,但将有6稍后。
谁能帮我明白什么是错在这里?
这是此功能:
公共静态牛逼ReadSharedData< T>(字符串文件名)其中T:类,新的()
{$ b $(b T)结果=新T();
VAR互斥= GetMutex(文件名);
mutex.WaitOne();
文件名= GetSharedFileName(文件名);
试
{
VAR存储= IsolatedStorageFile.GetUserStoreForApplication();使用
如果(storage.FileExists(文件名))
{
(VAR FILESTREAM = storage.OpenFile(文件名,FileMode.Open,FileAccess.Read))
{
按(VAR读者=新的StreamReader(FILESTREAM))
{
JSON字符串= reader.ReadToEnd();
如果(string.IsNullOrEmpty(JSON)==假)
{
VAR数据= JsonConvert.DeserializeObject< T>(JSON);
如果(数据!= NULL)
{
结果=数据;
}
}
}
}
}
}
赶上{}
终于
{
mutex.Release();
}
返回结果;
}
该问题发生在这条线:
VAR数据= JsonConvert.DeserializeObject< T>(JSON);
我MainPage.xaml.cs中
使用Microsoft.Phone.Shell;使用JSON_Storage_Test.Resources
;使用System.Diagnostics程序
;
使用System.Text;
使用System.IO.IsolatedStorage;
命名空间JSON_Storage_Test
{
公共部分类的MainPage:的PhoneApplicationPage
{
私人常量字符串文件名=movieSettings.json;
//构造
公众的MainPage()
{
的InitializeComponent();
}
私人无效SaveJ_Click(对象发件人,RoutedEventArgs E)
{
JSON字符串= @{
'名称':'坏男孩',
'RELEASEDATE':'1995-4-7T00:00:00,
'流派':'动作','喜剧']
};
FileStorage.WriteSharedData(movieSettings.json,JSON);
}
//林不知道在这里,我应该返回类型是
//我怎么会再访问检索到的数据
私人无效LoadJ_Click(对象发件人,RoutedEventArgs E)
{
FileStorage.ReadSharedData<&的MainPage GT;(文件名);
的Debug.WriteLine(加载点击);
}
}
}
您的问题是 - 正如你自己所说 - 你不知道该怎么做的返回值
FileStorage。 ReadSharedData<的MainPage>(文件名);
试图您的JSON字符串解析到类型的MainPage的对象应该具备哪些特性名称
, RELEASEDATE
和流派
(这应该是一个IEnumerable)。
您的MainPage
实际上有没有这样的属性,所以你的反序列化的崩溃。
尝试以下操作:
公共类移动
{
公共字符串名称;
公共字符串RELEASEDATE;
公开的IEnumerable<串GT;流派
}
然后调用 VAR deserializedMove = FileStorage.ReadSharedData< ;电影>(文件名);
I am trying to save and load files from isolatedStorage based on this class by forum member Shawn Kendrot that i got throughout this forum post.
I’m able to save with no problem but when loading then deserialising the json file I am getting an error
"A first chance exception of type 'Newtonsoft.Json.JsonSerializationException'
occurred in Newtonsoft.Json.DLL"
I don’t know what I could be doing wrong, because the file is saved and it’s reading properly but not deserializing. The json file has 4 entries at the moment, but it will have 6 later on.
Can anyone help me understand what is wrong here?
It's this function:
public static T ReadSharedData<T>(string fileName) where T : class, new()
{
T result = new T();
var mutex = GetMutex(fileName);
mutex.WaitOne();
fileName = GetSharedFileName(fileName);
try
{
var storage = IsolatedStorageFile.GetUserStoreForApplication();
if (storage.FileExists(fileName))
{
using (var fileStream = storage.OpenFile(fileName, FileMode.Open, FileAccess.Read))
{
using (var reader = new StreamReader(fileStream))
{
string json = reader.ReadToEnd();
if (string.IsNullOrEmpty(json) == false)
{
var data = JsonConvert.DeserializeObject<T>(json);
if (data != null)
{
result = data;
}
}
}
}
}
}
catch { }
finally
{
mutex.Release();
}
return result;
}
The problem is occurring on this line:
var data = JsonConvert.DeserializeObject<T>(json);
My MainPage.xaml.cs
using Microsoft.Phone.Shell;
using JSON_Storage_Test.Resources;
using System.Diagnostics;
using System.Text;
using System.IO.IsolatedStorage;
namespace JSON_Storage_Test
{
public partial class MainPage : PhoneApplicationPage
{
private const string FileName = "movieSettings.json";
// Constructor
public MainPage()
{
InitializeComponent();
}
private void SaveJ_Click(object sender, RoutedEventArgs e)
{
string json = @"{
'Name': 'Bad Boys',
'ReleaseDate': '1995-4-7T00:00:00',
'Genres': [ 'Action', 'Comedy' ]
}";
FileStorage.WriteSharedData("movieSettings.json", json);
}
//Im not sure here, what should the return type be
//and how would i then access the retrieved data
private void LoadJ_Click(object sender, RoutedEventArgs e)
{
FileStorage.ReadSharedData<MainPage>(FileName);
Debug.WriteLine("Load clicked");
}
}
}
Your problem is - as you pointed out by yourself - you have no clue what to do with the return value.
FileStorage.ReadSharedData<MainPage>(FileName);
tries to parse your json string to an object of type MainPage which should have the properties Name
, ReleaseDate
and Genres
(which should be an IEnumerable).
Your MainPage
actually has no such properties, so your deserialization crashes.
Try the following:
public class Move
{
public string Name;
public string ReleaseDate;
public IEnumerable<string> Genres
}
And then call var deserializedMove = FileStorage.ReadSharedData<Movie>(FileName);
这篇关于反序列化JSON C#"类型的第一次机会异常 - newtonsoft"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!