使用JSON中的冒号解析字段名称
如果json字段包含冒号(:),我们如何解析? 像这样:
{ "dc:creator":"Jordan, Micheal", "element:publicationName":"Applied Ergonomics", "element:issn":"2839749823" }
事实上,我想知道如何使用像restsharp这样的库进行映射?
使用Json.Net
string json = @"{ ""dc:creator"":""Jordan, Micheal"", ""element:publicationName"":""Applied Ergonomics"", ""element:issn"":""2839749823"" }"; var pub = JsonConvert.DeserializeObject(json);
public class Publication { [JsonProperty("dc:creator")] public string creator { set; get; } [JsonProperty("element:publicationName")] public string publicationName { set; get; } [JsonProperty("element:issn")] public string issn { set; get; } }
要么
Console.WriteLine(JObject.Parse(json)["dc:creator"]);
如果使用DataContractJsonSerializer
, DataMemberAttribute
具有属性Name
,可用于覆盖默认名称。 这意味着当您反序列化属性dc:creator
json值时dc:creator
被赋值为Publication::Creator
属性,相反,当您序列化C#对象时。
例如:
public class Publication { [DataMember(Name="dc:creator")] public string Creator { set; get; } [DataMember(Name="element:publicationName")] public string PublicationName { set; get; } [DataMember(Name="element:issn")] public string Issn { set; get; } }
如果你选择使用Json.Net
,@ LB的答案就是你要走的路。