简体   繁体   English

无法使用DataContractJsonSerializer将对象序列化为JSON

[英]Unable to Serialize Object to JSON Using DataContractJsonSerializer

I have taken the following code from https://msdn.microsoft.com/en-us/library/bb410770(v=vs.110).aspx and put it in a Visual Studio project. 我从https://msdn.microsoft.com/zh-cn/library/bb410770(v=vs.110).aspx中获取了以下代码,并将其放在Visual Studio项目中。

Program.cs Program.cs

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

namespace DataContractJsonSerializer_Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a person object.
            Person p = new Person();
            p.name = "John";
            p.age = 42;

            // Serialize the Person object to a memory stream using DataContractJsonSerializer.
            MemoryStream stream1 = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DataContractJsonSerializer));

            // Use the WriteObject method to write JSON data to the stream.
            ser.WriteObject(stream1, p);

            // Show the JSON output.
            stream1.Position = 0;
            StreamReader sr = new StreamReader(stream1);
            Console.Write("JSON form of Person object: ");
            Console.WriteLine(sr.ReadToEnd());
            Console.Read();
        }
    }
}

Person.cs 人.cs

using System.Runtime.Serialization;

namespace DataContractJsonSerializer_Example
{
    [DataContract]
    class Person
    {
        [DataMember]
        internal string name;

        [DataMember]
        internal int age;
    }
}

I receive the following runtime error: 我收到以下运行时错误:

An unhandled exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in System.Runtime.Serialization.dll

Additional information: Type 'System.Runtime.Serialization.Json.DataContractJsonSerializer' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.

The exception occurs at this line: 异常发生在此行:

ser.WriteObject(stream1, p);

That seems odd. 好像很奇怪 Rather than marking the Person class, it seems to want me to mark the DataContractJsonSerializer class itself. 似乎不是要标记Person类,而是要我标记DataContractJsonSerializer类本身。 Another odd thing is that I downloaded the sample code from MSDN and ran their version, which is basically the same as mine, without any trouble. 另一个奇怪的是,我从MSDN下载了示例代码并运行了它们的版本,该版本与我的版本基本相同,没有任何麻烦。 Theirs is a VS2010 project, and they have the Person class in the same file as the class containing the Main method, but that shouldn't be making a difference. 他们是一个VS2010项目,他们的Person类与包含Main方法的类位于同一文件中,但这不应该有所不同。 Can anyone tell me what I'm doing wrong? 谁能告诉我我在做什么错?

The issue is on line DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DataContractJsonSerializer)); 问题在于在线DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DataContractJsonSerializer)); it should be DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person)); 应该是DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用DataContractJsonSerializer作为JSON数组序列化对象 - Serialize an object using DataContractJsonSerializer as a json array 使用 DataContractJsonSerializer 将字典序列化为 JSON 对象 - Serialize a Dictionary as JSON object using DataContractJsonSerializer 如何使用 DataContractJsonSerializer 序列化包含日期和时间属性的 JSON 字符串? - How to serialize JSON string containing date and time property using DataContractJsonSerializer? JSON使用.NET DataContractJsonSerializer序列化器与字典进行序列化/反序列化 - JSON serialize/deserialize with Dictionary using .NET DataContractJsonSerializer serializer 如何使用DataContractJsonSerializer将类类型而不是命名空间序列化为Json字符串 - How to serialize class type but not the namespace to a Json string using DataContractJsonSerializer 使用DataContractJsonSerializer设置JSON对象root - Set JSON object root using DataContractJsonSerializer 如何使用DataContractJsonSerializer序列化/反序列化存储在对象字段中的DateTime? - How to serialize/deserialize a DateTime stored inside an object field using DataContractJsonSerializer? 在C#中使用DataContractJsonSerializer反序列化JSON对象 - Deserialization of JSON object by using DataContractJsonSerializer in C# 使用DataContractJsonSerializer对JSON对象进行部分反序列化 - Partial deserialization of JSON object by using DataContractJsonSerializer 使用DataContractJsonSerializer读取JSON - Read JSON using DataContractJsonSerializer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM