【发布时间】:2014-02-28 07:52:30
【问题描述】:
如果json字段包含冒号(:),我们如何解析?像这样:
{
"dc:creator":"Jordan, Micheal",
"element:publicationName":"Applied Ergonomics",
"element:issn":"2839749823"
}
事实上,我想知道如何使用 restsharp 之类的库来进行映射?
【问题讨论】:
如果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<Publication>(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 值时,将分配给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,@L.B 的答案就是要走的路。
【讨论】: