Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# JSON.NET写入无效的JSON?_C#_Silverlight_Serialization_Windows Phone 7_Json.net - Fatal编程技术网

C# JSON.NET写入无效的JSON?

C# JSON.NET写入无效的JSON?,c#,silverlight,serialization,windows-phone-7,json.net,C#,Silverlight,Serialization,Windows Phone 7,Json.net,看起来JSON.NET正在编写无效的JSON,尽管如果这是由于我的误用,我也不会感到惊讶 它似乎在重复JSON的最后几个字符: /* ... */ "Teaser":"\nfoo.\n","Title":"bar","ImageSrc":null,"Nid":44462,"Vid":17}]}4462,"Vid":17}]} 重复字符串为: 4462,"Vid":17}]} 我把它打印到控制台上,所以我不认为这是VisualStudio的文本可视化工具中的错误 序列化代码: s

看起来JSON.NET正在编写无效的JSON,尽管如果这是由于我的误用,我也不会感到惊讶

它似乎在重复JSON的最后几个字符:

/* ... */ "Teaser":"\nfoo.\n","Title":"bar","ImageSrc":null,"Nid":44462,"Vid":17}]}4462,"Vid":17}]}
重复字符串为:

4462,"Vid":17}]}
我把它打印到控制台上,所以我不认为这是VisualStudio的文本可视化工具中的错误

序列化代码:

       static IDictionary<int, ObservableCollection<Story>> _sectionStories;
       private static void writeToFile()
        {
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
            using (IsolatedStorageFileStream stream = storage.OpenFile(STORIES_FILE, FileMode.OpenOrCreate))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.Write(JsonConvert.SerializeObject(_sectionStories));
                }

            }

#if DEBUG
            StreamReader reader = new StreamReader(storage.OpenFile(STORIES_FILE, FileMode.Open));
            string contents = reader.ReadToEnd();

            JObject data = JObject.Parse(contents);
            string result = "";
            foreach (char c in contents.Skip(contents.Length - 20))
            {
                result += c;
            }
            Debug.WriteLine(result);

            // crashes here with ArgumentException
            // perhaps because JSON is invalid?
            var foo = JsonConvert.DeserializeObject<Dictionary<int, List<Story>>>(contents);
#endif
        }
static IDictionary\u sectionStories;
私有静态void writeToFile()
{
IsolatedStorageFile存储=IsolatedStorageFile.GetUserStoreForApplication();
使用(IsolatedStorageFileStream=storage.OpenFile(STORIES_FILE,FileMode.OpenOrCreate))
{
使用(StreamWriter=新StreamWriter(流))
{
Write(JsonConvert.SerializeObject(_sectionStories));
}
}
#如果调试
StreamReader=新的StreamReader(storage.OpenFile(STORIES_FILE,FileMode.Open));
字符串内容=reader.ReadToEnd();
JObject data=JObject.Parse(内容);
字符串结果=”;
foreach(contents.Skip中的字符c(contents.Length-20))
{
结果+=c;
}
Debug.WriteLine(结果);
//在这里发生意外
//也许是因为JSON无效?
var foo=JsonConvert.DeserializeObject(内容);
#恩迪夫
}
我做错什么了吗?还是这是一只虫子?是否有已知的解决方法

奇怪的是,
JObject.Parse()
没有抛出任何错误


我正在为Windows Phone 7构建Silverlight应用程序。

在编写您指定的文件时

FileMode.OpenOrCreate
如果文件存在,并且比要写入的数据长16字节(来自恰好以完全相同的数据结尾的较旧版本的数据),则当您完成写入新数据时,该数据仍将存在

解决方案:

FileMode.Create 
发件人:


Create:指定操作系统应创建新文件。如果文件已经存在,它将被覆盖

不清楚您指的是哪些文件在重复,以及底层结构是什么。您确定这不是对象的一部分吗???如何可能有一个重复的元素,而文本是反序列化的?我简直不敢相信。如果可以,请放入整个JSON.X-Ref: