如何在c#中反序列化json字符串?(How to deserialize json string in c#?)

编程入门 行业动态 更新时间:2024-10-26 04:20:11
如何在c#中反序列化json字符串?(How to deserialize json string in c#?)

我有一个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);

更多推荐

本文发布于:2023-07-27 15:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292427.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   序列化   如何在   json   deserialize

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!