我正在使用Newtonsoft.Json将不同的对象序列化为json字符串。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
<div class="container">
Website Title.
</div>
</header>
<div class="wrap">
<div class="block-1">
<div class="container">
<p>This pen was made to solve a problem on a project...</p>
</div>
</div>
<div class="block-2">
<div class="container">
<p>...I needed a sticky header with thr right bg colour.</p>
</div>
</div>
<div class="block-3">
<div class="container">
<p>But this conflicted with the footer, which was the same colour...</p>
</div>
</div>
<div class="block-4">
<div class="container">
<p>So the solution was to subtley change the header's bg on scroll</p>
</div>
</div>
</div>
它适用于我尝试过的所有类型的对象,除了一个。有问题的一个返回json字符串:“[]” 知道它可能是什么?没有错误。如果我删除了seetings,同样的结果。此类的所有属性都是公共的(但它从具有抽象属性的抽象类扩展)。空字符串的对象的值是:“{MyNamespace.CodeList&lt; MyNamespace.ContentType&gt;}”,而对于其他对象,值类似于:“{MyNamespace.SomeClass}”。这种差异究竟意味着什么?
答案 0 :(得分:0)
我猜测SerializeObject
正在挑选一个不受欢迎的转换器,尝试使用第三个参数强制它:
JsonConverter[] converters = ... // Pick of from http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_jsonconverter.htm#fullInheritance
string json = JsonConvert.SerializeObject(objectToWrite, seetings, converters);