Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.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# 使用Json.NET在windows phone 7中反序列化Json_C#_Windows Phone 7_Json.net - Fatal编程技术网

C# 使用Json.NET在windows phone 7中反序列化Json

C# 使用Json.NET在windows phone 7中反序列化Json,c#,windows-phone-7,json.net,C#,Windows Phone 7,Json.net,我在web服务器中有一个JSON,如下所示: {"My Book List": [{"ID":"5","TYPE":"History","TITLE":"Ekannoborti","PRICE":"200","IMAGE":"Ekannoborti.jpg","DOWNLOAD LINK":"http://www.starhostbd.com/"}],"success":3} 到目前为止,为了反序列化它,我已经做了以下工作: public class Attributes {

我在web服务器中有一个JSON,如下所示:

{"My Book List": [{"ID":"5","TYPE":"History","TITLE":"Ekannoborti","PRICE":"200","IMAGE":"Ekannoborti.jpg","DOWNLOAD LINK":"http://www.starhostbd.com/"}],"success":3} 
到目前为止,为了反序列化它,我已经做了以下工作:

 public class Attributes
    {
        public string ID{ get; set; }
        public string TYPE { get; set; }
        public string TITLE { get; set; }
        public string  PRICE { get; set; }
        public string IMAGE { get; set; }
        public string DOWNLOADLINK { get; set; }
    }

    public class DataJsonAttributeContainer
    {
        public List<Attributes> attributes { get; set; }
        //public Attributes attributes { get; set; }
    }

    public static T DeserializeFromJson<T>(string json)
    {
        T deserializedProduct = JsonConvert.DeserializeObject<T>(json);
        return deserializedProduct;
    }
公共类属性
{
公共字符串ID{get;set;}
公共字符串类型{get;set;}
公共字符串标题{get;set;}
公共字符串PRICE{get;set;}
公共字符串图像{get;set;}
公共字符串下载链接{get;set;}
}
公共类DataJsonAttributeContainer
{
公共列表属性{get;set;}
//公共属性{get;set;}
}
公共静态T反序列化FromJSON(字符串json)
{
T deserializedProduct=JsonConvert.DeserializeObject(json);
返回反序列化的产品;
}
&在我的代码中:

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        //var deserializedJSON = JsonConvert.DeserializeObject<Attributes>(e.Result);
        var container = DeserializeFromJson<DataJsonAttributeContainer>(e.Result);
        string asd = container.attributes[0].DOWNLOADLINK[0].ToString();
        //string asd = deserializedJSON.DOWNLOADLINK[0].ToString();
    } 
void webClient\u DownloadStringCompleted已完成(对象发送方,DownloadStringCompletedEventArgs e)
{
//var deserializedJSON=JsonConvert.DeserializeObject(e.Result);
var container=反序列化fromJSON(e.Result);
字符串asd=container.attributes[0]。下载链接[0]。ToString();
//字符串asd=deserializedJSON.DOWNLOADLINK[0].ToString();
} 

问题是:从debug wiindow中,我可以看到数据是在e.Result中分配的,但容器仍然为空。如何解决这个问题?请帮忙

JsonProperty
属性添加到
attributes
属性以匹配JSON中的属性名称,如下所示:

public class DataJsonAttributeContainer
{
    [JsonProperty("My Book List")]
    public List<Attributes> attributes { get; set; }
}
公共类DataJsonAttributeContainer
{
[JsonProperty(“我的书单”)]
公共列表属性{get;set;}
}

另外,您应该向
属性.DOWNLOADLINK
属性添加一个
JsonProperty
属性和“DOWNLOADLINK”值,以便它与JSON属性名称匹配。

乍一看,我认为问题出在DOWNLOADLINK属性上。 您的服务器返回“下载链接”,但您的属性名称中没有空格

您应该在属性中定义json表示,如下所示:

[JsonProperty(PropertyName = "DOWNLOAD LINK")]
public string DOWNLOADLINK { get; set; }

希望这对你有所帮助。

事实上,上述两个答案应该都能解决你的问题,你必须把它们加在一起

public class MyBookList
{
    public string ID { get; set; }
    public string TYPE { get; set; }
    public string TITLE { get; set; }
    public string PRICE { get; set; }
    public string IMAGE { get; set; }

    [JsonProperty("DOWNLOAD LINK")]
    public string DOWNLOADLINK { get; set; }
}

public class DataJsonAttributeContainer
{
    [JsonProperty("My Book List")]
    public List<MyBookList> MyBookList { get; set; }

    public int success { get; set; }
}
公共类MyBookList
{
公共字符串ID{get;set;}
公共字符串类型{get;set;}
公共字符串标题{get;set;}
公共字符串PRICE{get;set;}
公共字符串图像{get;set;}
[JsonProperty(“下载链接”)]
公共字符串下载链接{get;set;}
}
公共类DataJsonAttributeContainer
{
[JsonProperty(“我的书单”)]
公共列表MyBookList{get;set;}
公共int成功{get;set;}
}
而且

var container = DeserializeFromJson<DataJsonAttributeContainer>(e.Result);
string asd = container.attributes[0].DOWNLOADLINK.ToString();
var container=DeserializeFromJson(e.Result);
字符串asd=container.attributes[0]。DOWNLOADLINK.ToString();

试试这些课程。应该可以工作。

在这里工作得很好,但是我注意到上面的示例代码中有一个错误:缺少一个“在
我的书单之后
对不起!这是由于从实际的大json文件复制粘贴而导致的输入错误。实际文件是好的!你可以在这里检查我做了什么,它工作得很好:通过我和佩德罗的修改,这段代码适合我。我可以提取整个链接。但是您需要使用
string asd=container.attributes[0].DOWNLOADLINK.ToString()
以获取整个链接。在本例中,您只获得第一个字符。