反序列化后获取空对象C#Windows Phone

时间:2014-04-08 15:50:33

标签: c# json windows-phone-8 deserialization

我试图玩API。 API以JSON格式返回数据。 这是我提出请求的代码

private void makerequest()
    {
        HttpWebRequest request = HttpWebRequest.Create("http://grandtheftdata.com/bawsaq/api?a=DATA&o=JSON&p=PS3&t=2014-04-08T15:00:00Z," + now.ToString("YYYY-MM-DD") + "T" + now.ToString("HH:mm:ss") + "Z") as HttpWebRequest;
        request.Method = "GET";
        request.Accept = "application/json";
        request.ContentLength = 0;
        var playerResponse = request.BeginGetResponse(new AsyncCallback(getPlayer), request);
    }

    private void getPlayer(IAsyncResult ar)
    {
        try
        {
            string jsondata;
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)ar.AsyncState; ;
            using (HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.EndGetResponse(ar))
            {
                System.IO.Stream responseStream = response.GetResponseStream();
                using (var reader = new System.IO.StreamReader(responseStream))
                {
                    jsondata = reader.ReadToEnd();
                }
                responseStream.Close();
            }

            this.Dispatcher.BeginInvoke(() =>
            {
                PS3BAWSAQ.RootObject feed = Newtonsoft.Json.JsonConvert.DeserializeObject<PS3BAWSAQ.RootObject>(jsondata);
                MessageBox.Show(jsondata.Length + " characters");
            }
            );
        }
        catch (Exception e)
        {
            //Do something
        }
    }

它在jsondata变量中返回的JSON数据是

   {
    "data": {
        "PS3": {
            "2014-04-08T15:30:01Z": {
                "AMU": 3.42,
                "BDG": 6.73,
                "BET": 716.1,
                "BFA": 83.58,
                "BIN": 3.82,
                "BLE": 3591.3,
                "BRU": 4.5,
                "BTR": 3.51,
                "CNT": 50.15,
                "CRE": 2.5,
                "DGP": 100.16,
                "EYE": 3.68,
                "FAC": 3.49,
                "FRT": 3.86,
                "GOT": 6.09,
                "HAL": 3.58,
                "HVY": 1.45,
                "LSC": 1551.79,
                "LST": 1045.54,
                "LTD": 336.95,
                "MAI": 348.5,
                "PIS": 2.64,
                "PMP": 2143.31,
                "PON": 1700.82,
                "RON": 323.46,
                "SHK": 7.03,
                "SHR": 1261.47,
                "SHT": 582.65,
                "SPU": 1607.05,
                "SUB": 1509.58,
                "TNK": 5.56,
                "UMA": 4.47,
                "VAP": 6.86,
                "VOM": 1183.48,
                "WAP": 88.74,
                "WIW": 7.16,
                "WIZ": 1.28,
                "WZL": 999.98,
                "ZIT": 3.46
            }
        }
    },
    "status": {
        "code": 200,
        "text": "ok"
    }
}

json2csharp.com生成的类是

public class __invalid_type__20140324T144502Z
    {
        public double AMU { get; set; }
        public double BDG { get; set; }
        public double BET { get; set; }
        public double BFA { get; set; }
        public double BIN { get; set; }
        public double BLE { get; set; }
        public double BRU { get; set; }
        public double BTR { get; set; }
        public double CNT { get; set; }
        public double CRE { get; set; }
        public double DGP { get; set; }
        public double EYE { get; set; }
        public double FAC { get; set; }
        public double FRT { get; set; }
        public double GOT { get; set; }
        public double HAL { get; set; }
        public double HVY { get; set; }
        public double LSC { get; set; }
        public double LST { get; set; }
        public double LTD { get; set; }
        public double MAI { get; set; }
        public double PIS { get; set; }
        public double PMP { get; set; }
        public double PON { get; set; }
        public double RON { get; set; }
        public double SHK { get; set; }
        public double SHR { get; set; }
        public double SHT { get; set; }
        public double SPU { get; set; }
        public double SUB { get; set; }
        public double TNK { get; set; }
        public double UMA { get; set; }
        public double VAP { get; set; }
        public double VOM { get; set; }
        public double WAP { get; set; }
        public double WIW { get; set; }
        public double WIZ { get; set; }
        public double WZL { get; set; }
        public double ZIT { get; set; }
    }

    public class PS3
    {
        public __invalid_type__20140324T144502Z __invalid_name__2014 { get; set; }
    }

    public class Data
    {
        public PS3 PS3 { get; set; }
    }

    public class Status
    {
        public int code { get; set; }
        public string text { get; set; }
    }

    public class RootObject
    {
        public Data data { get; set; }
        public Status status { get; set; }
    }

问题是我无法正确地将JSON数据反序列化为PS3BAWSAQ对象。 result

我确定问题出在类名(无效类型)中。我更改了他们的名字,但这也没有帮助。 JSON有一个成员,它本身就是时间依赖的。

无法找到解决此问题的方法。请帮忙。

1 个答案:

答案 0 :(得分:0)

JsonConvert.Deserialize(string)方法返回一个动态对象。

这是您创建动态对象的地方

 dynamic x = JsonConvert.DeserializeObject(jsondata);

动态对象非常适合使用。您可以引用编译器不存在线索的属性。 You can read about them here

您必须像这样解析动态对象。

假设它只返回JSON中的上述对象之一

PS3BAWSAQ.RootObject obj = new PS3BAWSAQ.RootObject (){

   Prop1 = x.Key1,
   Prop2 = x.Key2,
   //And So on

};

如果它是一个对象列表,你可以这样做

foreach(PS3BAWSAQ.RootObject y in x){
    PS3BAWSAQ.RootObject obj = new PS3BAWSAQ.RootObject (){

       Prop1 = y.Key1,
       Prop2 = y.Key2,
       //And So on

    };
}

如果它的列表列表就像这样

foreach(var y in x.list1Name){
  foreach(PS3BAWSAQ.RootObject z in y){
    PS3BAWSAQ.RootObject obj = new PS3BAWSAQ.RootObject (){

       Prop1 = z.Key1,
       Prop2 = z.Key2,
       //And So on

    };
  }
}

在您的情况下,它看起来像是返回一个对象。所以你会想要选择一个。只需新建一个新容器并单独设置每个属性。 钥匙是敏感的!!!