Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataContractJsonSerializer返回空对象_C#_Json_Windows Phone 8 - Fatal编程技术网

C# DataContractJsonSerializer返回空对象

C# DataContractJsonSerializer返回空对象,c#,json,windows-phone-8,C#,Json,Windows Phone 8,我已经为这个问题挣扎了很长一段时间了,但解决不了它。 我有以下JSON字符串: {"Search":[{"Title":"somestring","Year":"somestring","imdbID":"somestring"}]}, {"Title":"somestring","Year":"somestring","imdbID":"somestring"} etc 字符串可以重复多次,因此我想将值存储在列表中。为此,我创建了以下两个类: SuggestionListener类: [D

我已经为这个问题挣扎了很长一段时间了,但解决不了它。 我有以下JSON字符串:

 {"Search":[{"Title":"somestring","Year":"somestring","imdbID":"somestring"}]}, {"Title":"somestring","Year":"somestring","imdbID":"somestring"} etc
字符串可以重复多次,因此我想将值存储在列表中。为此,我创建了以下两个类: SuggestionListener类:

[DataContract]
class SuggestionLister
{
    public List<MovieResults> suggestionlist {get;set;}
}
其中包含需要存储的数据。我尝试用以下代码反序列化它:

byte[] data = Encoding.UTF8.GetBytes(resp);
MemoryStream memStream = new MemoryStream(data);
DataContractJsonSerializer serializer = new    DataContractJsonSerializer(typeof(SuggestionLister));
SuggestionLister suggestionMovies = (SuggestionLister)serializer.ReadObject(memStream);
其中'resp'变量是JSON字符串。但是,当我尝试此代码时,suggestMovies对象仍然为空。怎么了?

试试看

[DataContract]
class SuggestionLister
{
   public List<MovieResults> Search {get;set;}
}
试一试


好的,这里有几个问题:

[DataContract]
public class SuggestionLister
{
     [DataMember]
    public List<MovieResults> Search { get; set; }
}
[DataContract]
公共类建议列表器
{
[数据成员]
公共列表搜索{get;set;}
}
列表属性中没有DataMember属性,它需要与数组值的名称匹配,即“搜索”


编辑:我用你的代码测试了所有这些。另外,您发布的JSON格式不正确,但我假设这是一个粘贴错误。

好的,因此有几个问题:

[DataContract]
public class SuggestionLister
{
     [DataMember]
    public List<MovieResults> Search { get; set; }
}
[DataContract]
公共类建议列表器
{
[数据成员]
公共列表搜索{get;set;}
}
列表属性中没有DataMember属性,它需要与数组值的名称匹配,即“搜索”


编辑:我用你的代码测试了所有这些。另外,您发布的JSON格式不正确,但我假设这是一个粘贴错误。

请确保,您的JSON仅此而已??是的,对不起,格式不太正确,这里有一个精确的副本:{“搜索”:[{“标题”:“盗梦空间”,“年份”:“2010”,“imdbID”:“tt1375666”},{“标题”:“盗梦空间:动作漫画”,“年份”:“2010”,“imdbID”:“tt1790736”},{“标题”:“盗梦空间:4Movie首映特别版”,“年份”:“2010”,“imdbID”:“tt1686778”},{“标题”:“WWA:The Inception”,“Year:“2001”,“imdbID:“tt0311992”},{“Title:“Inception”,“Year:“2010”,“imdbID:“tt1695201”}]}请确保,您的json仅此而已??它在这两者之间有不同的模式……是的,很抱歉格式不太正确这是一个完全相同的副本:“{”搜索“{”标题“:“Inception”,“Year:“2010”,“imdbID:“tt1375666”},{”标题“:”《盗梦空间:动作漫画》、《年份》:“2010”、“imdbID:“tt1790736”},{“标题”:“盗梦空间:4Movie首映特别版”、“年份”:“2010”、“imdbID:“tt1686778”},{“标题”:“WWA:盗梦空间”、“年份”:“tt0311992”},{“标题”:“盗梦空间”、“年份”:“2010”、“imdbID:“tt1695201”}'刚刚尝试过,结果相同。搜索属性仍然为null。您问题中的Json字符串是否是您正在使用的实际字符串?它看起来格式不正确。不,正如我在上面的评论中所述,它不是。很抱歉造成混淆,这是此链接的输出:刚刚尝试过,结果相同。搜索属性仍然为null。是吗e Json字符串在您的问题中您使用的实际字符串?看起来它的格式不正确。不,正如我在上面的评论中所说的,它不是。对于混淆,很抱歉,这是来自此链接的输出:谢谢!这与之前关于属性名称的评论相结合工作了!没有考虑它,对不起,感觉有点愚蠢很抱歉打扰您!没问题,很容易忽略,而且DataContractJsonSerializer有很多问题。如果您不想重命名实际属性,可以在DataMember属性中设置名称。
[DataMember(name=“Search”)]
谢谢!这与前面关于属性名称的评论相结合是有效的!不去想它会让你觉得有点愚蠢,很抱歉打扰了你!没问题,很容易忽略,而且DataContractJsonSerializer有很多问题。如果你不想重命名实际属性,可以在DataMem中设置名称ber属性。
[DataMember(Name=“Search”)]
[DataContract]
public class SuggestionLister
{
     [DataMember]
    public List<MovieResults> Search { get; set; }
}