Json.Net解析属性名称正确 [英] Json.Net Resolving Property Names Properly
问题描述
我得到的一些数据,看起来像下面的JSON从API
I am getting some data that looks like below JSON from an API
{
body_html: "<h1>Test</h1>",
id: "cu1bpkz",
link_id: "d3_3kkgis",
author: "jdoe",
author_flair_text: null,
author_flair_css_class: null,
parent_id: "t3_3kkgis",
body: "Test",
subreddit_id: "q5_39vkz",
created_utc: 1442108087,
subreddit: "test"
}
这是我的强类型类,我使用的反序列化:
And here is my Strongly-typed class that I use for deserialization:
public class Comment
{
public string AuthorFlairText { get; set; }
public string AuthorFlairCssClass { get; set; }
public string Author { get; set; }
public string LinkId { get; set; }
public string Id { get; set; }
public string BodyHtml { get; set; }
public string Url { get; set; }
public string SubredditId { get; set; }
public string Subreddit { get; set; }
public long CreatedUtc { get; set; }
public string Body { get; set; }
public string ParentId { get; set; }
}
这是我解决的属性名称解析:
This is my resolver for resolving the property names:
public class ApiContractResolver : DefaultContractResolver
{
protected override string ResolvePropertyName(string propertyName)
{
var parts = propertyName.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
return parts.Select(x => char.ToUpper(x[0]) + x.Substring(1)).Aggregate((curr, next) => curr + next);
}
}
这是我如何反序列化我的JSON,它不。工作
This is how I deserializing my JSON and it does not work.
var settings = new JsonSerializerSettings { ContractResolver = new ApiContractResolver() };
var obj = JsonConvert.DeserializeObject<Comment>(json, settings);
虽然像的身体和id的简单属性得到正确转换,更复杂的特性与_在道具名称做不。我缺少什么?
While simple properties like body and id get properly converted, more complex properties with _ in the prop name do not. What am I missing?
推荐答案
您可以试试这个。您可以使用 JsonPropertyAttribute
来告诉Json.Net属性所对应的JSON领域是什么。
You can try this. you can use JsonPropertyAttribute
to tell Json.Net what the property's corresponding json field is.
public class Comment
{
[JsonProperty("author_flair_text")]
public string AuthorFlairText { get; set; }
[JsonProperty("author_flair_css_class")]
public string AuthorFlairCssClass { get; set; }
[JsonProperty("author")]
public string Author { get; set; }
[JsonProperty("link_id")]
public string LinkId { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("body_html")]
public string BodyHtml { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("subreddit_id")]
public string SubredditId { get; set; }
[JsonProperty("subreddit")]
public string Subreddit { get; set; }
[JsonProperty("created_utc")]
public long CreatedUtc { get; set; }
[JsonProperty("body")]
public string Body { get; set; }
[JsonProperty("parent_id")]
public string ParentId { get; set; }
}
别的
一切都被照顾唯一的网址是我不能在JSON找物业。请看看别人的代码是准备好复制粘贴和运行。
Everything else has been taken care only URL is the property which I cannot find in the JSON. Please have a look else the code is ready to get copy paste and run.
您可以存储类型的对象 JsonModel
,并在模型的构造使用初始化 JsonConvert.DeserializeObject< T>
。你的公共属性,则可以只叫成 JsonModel
实例,并得到相应的值。
you could store an object of type JsonModel
and in your model's constructor initialize it using JsonConvert.DeserializeObject<T>
. Your public properties could then just call into that JsonModel
instance and get the appropriate values.
这篇关于Json.Net解析属性名称正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!