我在使用JSON.NET解析Last.FM API JSON时遇到问题。问题在于字符串值是空的,就像:http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=ASASAS&api_key=53aed44f6fa2ee83d40324232594e1d9&format=json
中的类似标签一样[JsonConverter(typeof(StringNullConverter<Tags>))]
public class Tags
{
public List<Tag> tag { get; set; }
}
internal class StringVacioConverter<T> : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(string)) || (objectType == typeof(List<string>));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String)
{
return null;
}
else { return serializer.Deserialize<T>(reader); }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
}
}
但是,这不起作用,它在尝试返回serializer时进入无限循环。反序列化......
答案 0 :(得分:2)
此代码适用于您的两个json字符串(只有 JsonConvert.DeserializeObject 需要一些自定义Converter的帮助(请参阅下面的 MyConverter )。)
var lastfm = LastFM.Deserialize(json);
public class LastFM
{
public class Image
{
[JsonProperty("#text")]
public string text { get; set; }
public string size { get; set; }
}
public class Stats
{
public string listeners { get; set; }
public string playcount { get; set; }
}
public class Similar
{
public List<Artist> artist { get; set; }
}
public class Tag
{
public string name { get; set; }
public string url { get; set; }
}
public class Tags
{
public List<Tag> tag { get; set; }
}
public class Link
{
[JsonProperty("#text")]
public string text { get; set; }
public string rel { get; set; }
public string href { get; set; }
}
public class Links
{
public Link link { get; set; }
}
public class Bio
{
public Links links { get; set; }
public string published { get; set; }
public string summary { get; set; }
public string content { get; set; }
}
public class Artist
{
public string name { get; set; }
public string mbid { get; set; }
public string url { get; set; }
public List<Image> image { get; set; }
public string streamable { get; set; }
public string ontour { get; set; }
public Stats stats { get; set; }
public Similar similar { get; set; }
public Tags tags { get; set; }
public Bio bio { get; set; }
}
public class RootObject
{
public Artist artist { get; set; }
}
public static RootObject Deserialize(string s)
{
return JsonConvert.DeserializeObject<RootObject>(s, new MyConverter());
}
class MyConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Similar) ||
objectType == typeof(Tags);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.Value is string && String.IsNullOrWhiteSpace((string)reader.Value)) return null;
return reader.Value;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
}
答案 1 :(得分:1)
尝试像这样调整转换器:
// I. here we inherit from more sophisticated class, created for us
internal class StringNullConverter<T> : CustomCreationConverter<T>
where T : new()
{
// II. thanks the new() constraint we can override this method easily
public override T Create(Type objectType)
{
return new T();
}
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(string)) || (objectType == typeof(List<string>));
}
// III. here we pass the rest to smart base
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String)
{
return null;
}
// this is the correct implementation
return base.ReadJson(reader, objectType, existingValue, serializer);
}
// unchanged
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
}
}
我们从Newtonsoft.Json
的全部力量中获利,这为我们带来了许多预先实施的东西