问题描述
林相当新的API和JSON,所以请多多包涵......
Im fairly new to API's and JSON so please bear with me...
我要消耗大约从去年FM的API专辑的信息,我可以消耗一切扎图像,似乎有一个命名的问题。
I want to consume information about Albums from last fm's API, i can consume everything bar the images as there seems to be a naming issue.
API的部分:
"image": [{
"#text": "http:\/\/userserve-ak.last.fm\/serve\/34s\/88057565.png",
"size": "small"
},
{
"#text": "http:\/\/userserve-ak.last.fm\/serve\/64s\/88057565.png",
"size": "medium"
},
{
"#text": "http:\/\/userserve-ak.last.fm\/serve\/174s\/88057565.png",
"size": "large"
},
{
"#text": "http:\/\/userserve-ak.last.fm\/serve\/300x300\/88057565.png",
"size": "extralarge"
},
{
"#text": "http:\/\/userserve-ak.last.fm\/serve\/_\/88057565\/Believe.png",
"size": "mega"
}],
通过JSON2CSharp运行JSON后,我得到:
After running the JSON through JSON2CSharp I get:
public class Image
{
public string __invalid_name__#text { get; set; }
public string size { get; set; }
}
这显然是不行的,所以任何人都可以点我的方式来解决我的问题朝着正确的方向?
是否有可能将其重命名?
Obviously this is no good, So could anyone point me in the right direction of a way to solve my issue?Is it possible to rename them?
任何帮助将大大AP preciated,感谢:)
Any help would be greatly appreciated, Thanks :)
推荐答案
我用Json.NET解析他们的API响应,并使用JsonProperty属性#text映射到一个有效的属性名称,如下所示:
I used Json.NET for parsing their API responses, and used the JsonProperty attribute to map #text to a valid property name, as follows:
public class ArtistImage
{
[JsonProperty("size")]
public string Size { get; set; }
[JsonProperty("#text")]
public string Uri { get; set; }
}
我的code $上的C $ CPLEX如果你想让它:
但基本上,一旦你有你的类中定义的,你只需用JsonSerializer进行反序列化(jsonData)。
My code's on CodePlex if you want it: https://concertfinder.codeplex.com/But basically once you have your classes defined you just use a JsonSerializer to Deserialize(jsonData).
这篇关于使用LastFM等API JSON命名问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!