2017-08-01 79 views
1

每次有名字中带有“&”符号的艺术家出现时,我会在我的标签“R3HAB VINAI”而不是“R3HAB & VINAI”上获得整个字符串,因为它是对最终用户更有用。奇怪的是,“&”符号显示在曲目名称中很好。也许有人可以告诉我我做错了什么。从user.getRecentTracks艺术家缺少字符串中的字符

last.fm JSON API数据

{ 
recenttracks:{ 
    track:[ 
     { 
      artist:{ 
       #text:"R3HAB & VINAI", 
       mbid:"eb0fbe2c-6eb7-40bc-900b-3e056c158125" 
      }, 
      name:"HOW WE PARTY", 
      streamable:"0", 
      mbid:"b410da12-f308-4e2f-af88-19c69312e450", 
      album:{ 
       #text:"", 
       mbid:"" 
      }, 
      url:"https://www.last.fm/music/R3HAB+&+VINAI/_/HOW+WE+PARTY", 
      image:[ 
       { 
        #text:"", 
        size:"small" 
       }, 
       { 
        #text:"", 
        size:"medium" 
       }, 
       { 
        #text:"", 
        size:"large" 
       }, 
       { 
        #text:"", 
        size:"extralarge" 
       } 
      ], 
      @attr:{ 
       nowplaying:"true" 
      } 
     }, 
     { 
      artist:{ 
       #text:"Alan Walker", 
       mbid:"" 
      }, 
      name:"Sing me to Sleep", 
      streamable:"0", 
      mbid:"", 
      album:{ 
       #text:"", 
       mbid:"" 
      }, 
      url:"https://www.last.fm/music/Alan+Walker/_/Sing+me+to+Sleep", 
      image:[ 
       { 
        #text:"", 
        size:"small" 
       }, 
       { 
        #text:"", 
        size:"medium" 
       }, 
       { 
        #text:"", 
        size:"large" 
       }, 
       { 
        #text:"", 
        size:"extralarge" 
       } 
      ], 
      date:{ 
       uts:"1501613749", 
       #text:"01 Aug 2017, 18:55" 
      } 
     } 
    ], 
    @attr:{ 
     user:"SomeName", 
     page:"1", 
     perPage:"1", 
     totalPages:"8676", 
     total:"8676" 
    } 
} 
} 

C#类上市

public class Rootobject 
    { 
     public Recenttracks recenttracks { get; set; } 
    } 

    public class Recenttracks 
    { 
     public Track[] track { get; set; } 
     public Attr attr { get; set; } 
    } 

    public class Attr 
    { 
     public string user { get; set; } 
     public string page { get; set; } 
     public string perPage { get; set; } 
     public string totalPages { get; set; } 
     public string total { get; set; } 
    } 

    public class Track 
    { 
     public Artist artist { get; set; } 
     public string name { get; set; } 
     public string streamable { get; set; } 
     public string mbid { get; set; } 
     public Album album { get; set; } 
     public string url { get; set; } 
     public Image[] image { get; set; } 
     public Attr1 attr { get; set; } 
     public Date date { get; set; } 
    } 

    public class Artist 
    { 
     [JsonProperty("#text")] 
     public string text { get; set; } 
     public string mbid { get; set; } 
    } 

    public class Album 
    { 
     public string text { get; set; } 
     public string mbid { get; set; } 
    } 

    public class Attr1 
    { 
     public string nowplaying { get; set; } 
    } 

    public class Date 
    { 
     public string uts { get; set; } 
     public string text { get; set; } 
    } 

    public class Image 
    { 
     public string text { get; set; } 
     public string size { get; set; } 
    } 
+0

WHE你是否显示这些标签?在网页上?的WinForms?两者都需要转义,但以不同的方式 –

+0

像WinForm标签一样?我相信你需要将'&'加倍。所以用'&&'替换'&'。 https://stackoverflow.com/questions/4325094/enter-symbol-into-a-text-label-in-windows-forms – TyCobb

+0

取决于你如何显示字符串。我们需要知道它是一个winform,xml,html等,根据你的情况'&'的解释是不同的。 –

回答

4

您很可能需要转义符号。

  • 如果你使用Windows窗体,并在标签显示出来,可以在技术上做到不逃避的符号,在标签上的UseMnemonic特性切换到false
  • 如果您使用的是一个标签它可以与TextFormatFlag做,如果你不想逃避它
  • 可以躲开这个角色,通过使用&&
  • 任何其他如果你正在展示它在网络上,你会需要它使用HttpUtility.HtmlEncode()或全部切换到&&
  • 如果你喜欢另一种方式来对其进行编码,可以使用WebUtility.HtmlEncode()不具有相关性,以逃脱System.Web
+1

想你的意思''&;) – TyCobb

+0

我做到了,谢谢你笑 –

0

当显示到你需要逃跑的符号来显示它一个WinForm。您可以用&&替换它的所有匹配项。

之前设置你的艺术家,对字符串进行这种含它:

artist = artist.Replace("&", "&&");

2

默认情况下,连字符(&)被解释为“下划线后面的字符,并使其成为键盘快捷键。” (当然做显示的符号。)所以,你有两个选择:要么

  • 设置标签的UseMnemonic属性false,告诉它不要试图解释任何&符号,或

  • 用两个“&”符号替换每个“&”符号,这是一个特殊的序列,意思是“只留下这个&符号”。 (当然只显示一个符号)。