我有一个json字符串:
[{"Id":[1], "Value":"MyText" }, {"Id":[20, 31], "Value":"AnotherText" },{"Id":[2, 3, 4, 5], "Value":"MyText"}]我想解析它(我的json sting已经在byte []数组中):
private class MyClass { public int[] Id { get; set; } public string Value { get; set; } } var stream= new MemoryStream(jsonStr); var ser = new DataContractJsonSerializer(typeof(MyClass)); var result = (MyClass) ser.ReadObject(stream);但我得到了例外:
Message "Type 'MyNameSpace.Test+MyClass' 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. See the Microsoft .NET Framework documentation for other supported types."这有什么不对吗?
更新
我要编辑我的课程:
[DataContract] private class MyClass { [DataMember] public int[] Id { get; set; } [DataMember] public string Value { get; set; } }我怎么没有得到任何exeptions但反序列化后我得到空字段的对象。
UPDATE2
当我尝试解析json字符串时:
{"Id":[1], "Value":"MyText" }我的代码工作正常。 但是如何反序列化这样的对象数组:
[{"Id":[1], "Value":"MyText" },{"Id":[2,6], "Value":"MyText2222" },{"Id":[3,4], "Value":"MyText1111" }]I have a json string:
[{"Id":[1], "Value":"MyText" }, {"Id":[20, 31], "Value":"AnotherText" },{"Id":[2, 3, 4, 5], "Value":"MyText"}]I want to parse it (my json sting already in byte[] array):
private class MyClass { public int[] Id { get; set; } public string Value { get; set; } } var stream= new MemoryStream(jsonStr); var ser = new DataContractJsonSerializer(typeof(MyClass)); var result = (MyClass) ser.ReadObject(stream);But i get exeption:
Message "Type 'MyNameSpace.Test+MyClass' 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. See the Microsoft .NET Framework documentation for other supported types."Whats wrong here?
Update
I'd edit my class:
[DataContract] private class MyClass { [DataMember] public int[] Id { get; set; } [DataMember] public string Value { get; set; } }How i not get any exeptions but after deserialize i get object with empty fields.
Update2
When i tried parse json string:
{"Id":[1], "Value":"MyText" }my code works fine. But how to deserialize a array of objects like this:
[{"Id":[1], "Value":"MyText" },{"Id":[2,6], "Value":"MyText2222" },{"Id":[3,4], "Value":"MyText1111" }]最满意答案
DataContractJsonSerializer要求必须使用DataContract属性(以及具有DataMember的属性)标记类,如下所示:
[DataContract] public class Person { [DataMember] public int Id { get; set; } [DataMember] public string Name { get; set; } }或者使用不需要属性的JSON.NET库,并且语法非常简单:
JsonConvert.DeserializeObject(json);DataContractJsonSerializer requires that classes must be marked with DataContract attribute(and properties with DataMember), like this:
[DataContract] public class Person { [DataMember] public int Id { get; set; } [DataMember] public string Name { get; set; } }Or use JSON.NET libraries, which doesn't require attributes, and has very easy syntax:
JsonConvert.DeserializeObject(json);更多推荐
发布评论