Parsing field name with a colon in JSON

前端 未结 2 2107
天命终不由人
天命终不由人 2020-12-04 00:41

How can we parse if json fields contains a colon(:)? Like this:

{
  \"dc:creator\":\"Jordan, Micheal\",
  \"element:publicationName\":\"Applied Ergonomics\"         


        
2条回答
  •  抹茶落季
    2020-12-04 01:02

    If you use DataContractJsonSerializer, DataMemberAttribute has property Name which can be used to override default name. This means that when you deserialize json value of property dc:creator is assigned to Publication::Creator property and on the contrary when you serialize C# object.

    For example:

    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; }
    }
    

    If you choose to use Json.Net, @L.B's answer is the way to go.

提交回复
热议问题