Tag: json.net

JSonNet布尔序列化

快速提问: 在JSONNet中 – 如何将bool true / false序列化为bool 1/0 我可以看到我们如何处理空值,所有这些似乎无法找到如何做到这一点。 这可能吗?

JSON.NET DeserializeObject到对象列表

我正在尝试使用JSON.NET lib将对象反序列化为对象列表。 我的json文件是: [ { “id”: 1, “name”: “Poczta”, “description”: “Opis”, “latitude”: 52.25197, “longitude”: 20.896355, “accuracy”: 0, “type”: “”, “image”: null }, { “id”: 2, “name”: “WAT”, “description”: “Budynek główny – sztab.\r\nzażółć gęślą jaźń”, “latitude”: 52.2531213, “longitude”: 20.8995849, “accuracy”: 0, “type”: “Uczelnia”, “image”: null }, { “id”: 3, “name”: “Przychodnia”, “description”: “Opis”, “latitude”: 52.250808, “longitude”: […]

有没有办法将Json.Net反序列化与不可变类一起使用?

我正在使用一个使用json的API。 我有一些我为API建模而创建的类。 为了简化生活,我的模型使用公共属性,当将json反序列化为对象时,Json.Net将使用这些属性。 我想让我的对象不可变,但我遇到了一个问题,因为如果我将我的属性设为只读,我就会破坏反序列化。 有没有办法让我拥有不可变对象,并使用反序列化?

在JSON.NET中序列化派生类时的字段顺序

考虑这两个类: public Class Base { public string Id {get; set;} public string Name {get; set;} public string LastName {get; set;} } 派生类: public Class Derived : Base { public string Address {get; set;} public DateTime DateOfBirth {get; set;} } 使用Json.Net序列化Derived类 时 : Derived record = new Derived record(); {// Initialize here…} JsonConvert.SerializeObject(record); 默认情况下, Derived类的属性首先出现: { […]

如何为Json.NET输出添加注释?

有没有办法可以自动为JSON.Net的序列化输出添加注释? 理想情况下,我认为它类似于以下内容: public class MyClass { [JsonComment(“My documentation string”)] public string MyString { get; set; } } 或者(如果可以避免注释,则更好): public class MyClass { /// /// My documentation string /// public string MyString { get; set; } } 会产生: { //My documentation string “MyString”: “Test” } 我问的原因是我们使用Json.NET来序列化配置文件,以后可以手动更改。 我想在我的C#配置类中包含文档,并在JSON中重现这些文档,以帮助以后可能需要更改文件的人。 更新 :正如RoToRa在下面指出的那样,在JSON规范中技术上不允许发表评论(请参阅http://www.json.org上的方便语法图)。 但是, Json.NET站点上的function表包括: 支持阅读和撰写评论 和Newtonsoft.Json.JsonTextWriter.WriteComment(string)存在,它输出一个注释。 我对创建注释的简洁方法感兴趣,而不是直接使用JsonTextWriter 。

如何使用JSON.net解析JSON文件

我试图读取一个JSON文件并解析它。 我有这个代码从我的文件中读取 StreamReader re = new StreamReader(“artists.json”); JsonTextReader reader = new JsonTextReader(re); 但是我现在如何从读者解析它以便我可以从文件中搜索数据? 我试过阅读文档但找不到任何东西

分配后,静态属性为null

我有这个代码: static class Global { public static readonly IChannelsData Channels = new ChannelsData(); public static readonly IMessagesData Messages = new MessagesData(); } 我的理解是,因为这个类是静态的,所以Global.Channels或Global.Messages不可能在它们被赋予实例的情况下为null。 但是,我尝试访问该属性 public class Channel : IComparable { … private SortedList _messages; [JsonConstructor] public Channel() { _messages = new SortedList(); } [OnDeserialized] private void Init(StreamingContext context) { **Global.Channels.RegisterChannel(this);** } … } 我在Global.Channels上得到一个NullReferenceException ,我已经在即时窗口中确认了。 […]

使用JsonConvert.DeserializeObject反序列化派生对象列表

我有这个对象 public class ConversationAPI { [JsonProperty(PropertyName = “lU”)] public DateTime LastUpdated { get; set; } [JsonProperty(PropertyName = “m”, TypeNameHandling = TypeNameHandling.All)] public List Messages { get; set; } } 我作为json从API发送,并在我的客户端应用程序中反序列化。 The List Messages property contains either [Serializable] public class Message { [JsonProperty(PropertyName = “t”)] public string Text { get; set; } [JsonProperty(PropertyName = “ty”)] public […]

Json.Net可以嵌入到可执行文件中吗?

我将Netwonsoft.Json库的’Embed Interop Types’属性设置为true ,它返回一个错误: Cannot embed interop types from assembly ‘c:\path\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll’ because it is missing either the ‘ImportedFromTypeLibAttribute’ attribute or the ‘PrimaryInteropAssemblyAttribute’ attribute c:\path\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll 它看起来像在Newtonsoft.Json库中寻找缺少的引用,但我不完全确定。 是否有可能将Json.Net嵌入到可执行文件中?

无法反序列化当前的JSON对象(例如{“name”:“value”})

这是我的JSON数据 { “logInResult”: [ { “Name”: “yogesh singh”, “cityName”: “”, “img”: “DefaultImage/D_Vp_Men.png”, “usrId”: “374” } ] } 这是我的代码 public async Task Index() { HttpClient webClient1 = new HttpClient(); Uri uri = new Uri(“http://m.vinipost.com/service/userprofile.svc/logIn?loginId=thyschauhan@gmail.com&pass=12345”); HttpResponseMessage response1; response1 = await webClient1.GetAsync(uri); var jsonString = await response1.Content.ReadAsStringAsync(); var _Data = JsonConvert.DeserializeObject<List>(jsonString); foreach (JClass Student in _Data) { ViewBag.Message […]