如何使用System.Runtime.Serialization.Json解析此String?

编程入门 行业动态 更新时间:2024-10-22 15:32:09
本文介绍了如何使用System.Runtime.Serialization.Json解析此String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您能帮我如何使用System.Runtime.Serialization.Json(不是Json.NET )获取此数组中每本书的信息:

Can you help me how to use System.Runtime.Serialization.Json (not Json.NET) to get information of each book in this tring to array:

{ "books": [ {"name":"Book 1","orig":"Author 1","date":2009,"lang":"en"}, {"name":"Book 2","orig":"Author 2","date":2012,"lang":"fr"} ], "src":"lib", "id":212 }

推荐答案

以下是我整理的一个快速示例,看起来很有效:

Here's a quick sample I whipped up which appears to work:

using System; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; class Test { static void Main() { using (Stream stream = File.OpenRead("test.json")) { var serializer = new DataContractJsonSerializer(typeof(Library)); Library library = (Library) serializer.ReadObject(stream); Console.WriteLine(library.Books[0].Name); } } } [DataContract] class Book { [DataMember(Name="name")] public string Name { get; set; } [DataMember(Name="orig")] public string Orig { get; set; } [DataMember(Name="date")] public string Date { get; set; } [DataMember(Name="lang")] public string Lang { get; set; } } [DataContract] class Library { [DataMember(Name="books")] public IList<Book> Books { get; set; } [DataMember(Name="src")] public string Src { get; set; } [DataMember(Name="id")] public string Id { get; set; } }

我确定您还有很多其他可以调整的选项,但这至少应该可以让您入门.

I'm sure there are plenty of other options you can tweak, but that should at least get you started.

更多推荐

如何使用System.Runtime.Serialization.Json解析此String?

本文发布于:2023-11-10 02:22:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574079.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   System   Runtime   String   Json

发布评论

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

>www.elefans.com

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