简体   繁体   English

对如何在C#中使用JSON感到困惑

[英]Confused about how to use JSON in C#

The answer to just about every single question about using C# with JSON seems to be "use JSON.NET", but that's not the answer I'm looking for. 关于使用C#和JSON的每一个问题的答案似乎是“使用JSON.NET”,但这不是我正在寻找的答案。

The reason I say that is, from everything I've been able to read in the documentation, JSON.NET is basically just a better performing version of the DataContractSerializer built into the .NET framework... 我说的原因是,从我在文档中可以阅读的所有内容来看,JSON.NET基本上只是构建在.NET框架中的DataContractSerializer的更好表现版本...

Which means if I want to deserialize a JSON string, I have to define the full, strongly-typed class for EVERY request I might have. 这意味着如果我想反序列化一个JSON字符串,我必须为我可能拥有的每个请求定义完整的强类型类。 So if I have a need to get categories, posts, authors, tags, etc., I have to define a new class for every one of these things. 因此,如果我需要获取类别,帖子,作者,标签等,我必须为这些内容中的每一个定义一个新类。

This is fine if I built the client and know exactly what the fields are, but I'm using someone else's API, so I have no idea what the contract is unless I download a sample response string and create the class manually from the JSON string. 如果我构建了客户端并确切知道字段是什么,这很好,但是我正在使用别人的API,所以我不知道合同是什么,除非我下载一个示例响应字符串并从JSON字符串手动创建类。

Is that the only way it's done? 这是唯一的方法吗? Is there not a way to have it create a kind of hashtable that can be read with json["propertyname"]? 有没有办法让它创建一种可以用json [“propertyname”]读取的哈希表?

Finally, if I do have to build the classes myself, what happens when the API changes and they don't tell me (as twitter seems to be notorious for doing)? 最后,如果我自己必须自己构建类,那么当API发生变化而他们没有告诉我(因为Twitter似乎因为这样做而臭名昭着)时会发生什么? I'm guessing my entire project will break until I go in and update the object properties... 我猜我的整个项目都会破坏,直到我进入并更新对象属性...

So what exactly is the general workflow when working with JSON? 那么使用JSON时的一般工作流程到底是什么? And by general I mean library-agnostic. 一般来说,我的意思是与图书馆无关。 I want to know how it's done in general, not specifically to a target library... 我想知道它是如何完成的,而不是专门针对目标库......

It is very hard to be library-agnostic as you request because how you work with json really depends on the library you use. 您要求与库无关是非常困难的,因为您使用json的方式实际上取决于您使用的库。 As an example inside JSON.NET there are multiple ways you could work with JSON. 作为JSON.NET中的一个示例,有多种方法可以使用JSON。 There is the method you talk about with direct serialization into objects. 您可以通过直接序列化到对象来讨论该方法。 That is type safe but will break if the data from your API changes. 这是类型安全的,但如果API中的数据发生更改,则会中断。 However, there is also a LINQ-to-JSON that provides a JObject (which behaves fairly similarly to XElement) that provides a way to do JObject["key"] as you requested in your question. 但是,还有一个LINQ-to-JSON提供了一个JObject(其行为与XElement非常相似),提供了一种方法来按照您在问题中的要求执行JObject [“key”]。 If you are really just looking for a flexible way to work with JSON inside C#, then check out JSON.NET's LINQ-to-JSON. 如果您真的只是想在C#中使用JSON的灵活方式,那么请查看JSON.NET的LINQ-to-JSON。

In reality no matter how you do it, if the API changes your code is likely to break. 实际上,无论你如何做,如果API改变你的代码可能会破坏。 Even if you are just strictly a hashtable-based approach, your code will still be likely to break if the data coming back changes. 即使您只是基于哈希表的方法,如果数据返回发生变化,您的代码仍可能会中断。

Edit 编辑

JSON.NET Documentation JSON.NET文档

Examples 例子

If you check out the examples, the second one should give you a good example of how LINQ-to-JSON works. 如果您查看示例,第二个应该为您提供LINQ-to-JSON如何工作的一个很好的示例。 It allows you to work with it without defining any classes. 它允许您使用它而无需定义任何类。 Everything gets converted to standard framework classes (mostly collections and strings). 一切都转换为标准框架类(主要是集合和字符串)。 This avoids the need to maintain classes. 这避免了维护类的需要。

I've been a Perl developer for over a decade, and I've just recently started to work in C#. 十多年来我一直是Perl开发人员,我最近才开始使用C#。 I'm surprised by how much I like it (I don't like Java at all) but one of the most difficult cognitive switches is going from "Everything can be treated as a string and the language takes care of conversions" to "Pre-define your types." 我对它的喜欢程度感到惊讶(我根本不喜欢Java),但最困难的认知转换之一就是从“一切都可以被视为一个字符串,语言处理转换”到“Pre” - 定义你的类型。“ In this case string-thinking might be an advantage, because it's what you need to do for the kind of API you're asking for. 在这种情况下,字符串思考可能是一个优势,因为这是您需要为您要求的API类型所做的事情。

You need to write a JSON parser that understands the syntax , which is fairly simple: comma-separated lists, key/value pairs, {} for hashes/objects, [] for arrays, and quoting/escaping constructs. 您需要编写一个理解语法的JSON解析器,这非常简单:以逗号分隔的列表,键/值对,{}用于散列/对象,[]用于数组,以及引用/转义构造。 You'll want to create a Hashtable to start because the top-level entity in JSON is always an object, then scan the JSON string character-by-character. 您将要创建一个Hashtable,因为JSON中的顶级实体始终是一个对象,然后逐个字符地扫描JSON字符串。 Pull out key/value pairs; 拉出键/值对; if the value starts with { then add it as a new Hashtable, if it starts with [ add it as a new ArrayList, otherwise add it as a string. 如果值以{开头,则将其添加为新的Hashtable,如果它以[将其添加为新的ArrayList开头,否则将其添加为字符串。 If you get { or [ you'll need to recursively descend to add the child data elements. 如果你得到{或[你需要以递归方式下降来添加子数据元素。

If .NET has a good recursive descent parser, you could probably use that to make the job simpler or more robust, but JSON is simple enough to make this a good and reasonably completable exercise. 如果.NET有一个很好的递归下降解析器,你可以使用它来使工作更简单或更健壮,但JSON很简单,足以使它成为一个好的,可合理完成的练习。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM