问题描述
我正在上访问TMDB的应用程序。我用的是DataContractJsonSerializer类读取由TMDB API返回的数据。
还有就是API的一种方法,它返回一个JSON可以是这样的:
{ID:550,最爱:假的,评为:假的,监视列表:假}
或类似:
{ID:49521,最爱:假的,评为:{值:5.5},监视列表:假}
正如你所看到的,在第一个JSON额定字段是一个布尔值,并在第二个事实并非如此。
有没有好的办法来处理这个JSONs?
感谢您的帮助
编辑:阅读JSON:
[DataContract]
公共类JsonModelBase< T>其中T:类,新的()
{
公共牛逼DesterilizeJson(字符串jsonContent)
{
VAR字节= Encoding.UTF8.GetBytes(jsonContent);
使用(VAR流=新的MemoryStream(字节))
返回DesterilizeJson(流);
}
公共牛逼DesterilizeJson(流jsonStream)
{
无功序列化=新DataContractJsonSerializer(typeof运算(T));
返程(T)serializer.ReadObject(jsonStream);
}
}
[DataContract]
公共类TmdbMovieAccountStates:JsonModelBase< TmdbMovieAccountStates>
{
[数据成员(NAME =ID)
公众的Int32标识{获取;组; }
[数据成员(名称为最爱)
公共布尔喜爱{获得;组; }
[数据成员(NAME =评为)
公共布尔额定{获得;组; }
[数据成员(名称为监视列表)]
公共布尔监视列表{获得;组; }
}
样品(如果你想尝试的东西了)
VAR JSON ={\ID \:49521,\最喜欢的\:假的,\评为\:{\价值\:5.5} \ 监视列表\:假};
VAR字节= Encoding.UTF8.GetBytes(JSON);
使用(VAR流=新的MemoryStream(字节))
{
无功序列化=新DataContractJsonSerializer(typeof运算(TmdbMovieAccountStates));
VAR数据=(TmdbMovieAccountStates)serializer.ReadObject(流);
}
在使用动态类型时,我会用 Json.Net 代替DataContractJsonSerializer。
VAR OBJ = JsonConvert.DeserializeObject< TmdbMovieAccountStates>(JSON);
如果(obj.Rated是布尔)
Console.WriteLine(obj.Rated);
其他
Console.WriteLine(obj.Rated.value);
公共类TmdbMovieAccountStates
{
公众的Int32标识{获取;组; }
公共布尔喜爱{获得;组; }
公共动态额定{获得;组; }
公共布尔监视列表{获得;组; }
}
您甚至不需要声明任何类
动态OBJ = JObject.Parse(JSON);
如果(obj.rated是布尔)
Console.WriteLine(obj.rated);
其他
Console.WriteLine(obj.rated.value);
I’m working on an application which accesses the TMDb.I use the DataContractJsonSerializer class to read the data returned by the TMDb API.
There is one method of the API which returns a JSON which can look like:
{"id":550,"favorite":false,"rated":false,"watchlist":false}
or like:
{"id":49521,"favorite":false,"rated":{"value":5.5},"watchlist":false}
As you can see, the rated field in the first JSON is a Boolean and in the second one it isn’t.
Is there a good way to deal with this JSONs?
Thanks for any Help
Edit:Reading the JSON:
[DataContract]
public class JsonModelBase<T> where T : class, new()
{
public T DesterilizeJson(String jsonContent)
{
var bytes = Encoding.UTF8.GetBytes(jsonContent);
using (var stream = new MemoryStream(bytes))
return DesterilizeJson(stream);
}
public T DesterilizeJson(Stream jsonStream)
{
var serializer = new DataContractJsonSerializer(typeof(T));
return (T)serializer.ReadObject(jsonStream);
}
}
[DataContract]
public class TmdbMovieAccountStates : JsonModelBase<TmdbMovieAccountStates>
{
[DataMember(Name = "id")]
public Int32 Id { get; set; }
[DataMember(Name = "favorite")]
public Boolean Favorite { get; set; }
[DataMember(Name = "rated")]
public Boolean Rated { get; set; }
[DataMember(Name = "watchlist")]
public Boolean Watchlist { get; set; }
}
Sample (if you want to try something out)
var json = "{\"id\":49521,\"favorite\":false,\"rated\":{\"value\":5.5},\"watchlist\":false}";
var bytes = Encoding.UTF8.GetBytes(json);
using ( var stream = new MemoryStream( bytes ) )
{
var serializer = new DataContractJsonSerializer(typeof(TmdbMovieAccountStates));
var data = (TmdbMovieAccountStates)serializer.ReadObject(stream);
}
When dealing with dynamic types I would use Json.Net instead of DataContractJsonSerializer.
var obj = JsonConvert.DeserializeObject<TmdbMovieAccountStates>(json);
if (obj.Rated is Boolean)
Console.WriteLine(obj.Rated);
else
Console.WriteLine(obj.Rated.value);
public class TmdbMovieAccountStates
{
public Int32 Id { get; set; }
public Boolean Favorite { get; set; }
public dynamic Rated { get; set; }
public Boolean Watchlist { get; set; }
}
You don't even need to declare any class
dynamic obj = JObject.Parse(json);
if (obj.rated is bool)
Console.WriteLine(obj.rated);
else
Console.WriteLine(obj.rated.value);
这篇关于阅读使用DataContractJsonSerializer JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!