【问题标题】:How to access returned object from different function & class如何从不同的函数和类访问返回的对象
【发布时间】:2015-04-02 13:28:19
【问题描述】:

我有一个 c# 应用程序,一旦您按下按钮,它就会向 Web API 发送一个 Web 请求并检索所有 Json 值。然后将它们反序列化为一个对象并返回到调用函数的位置。

现在我正在尝试访问从另一个类中的 GetApi 函数返回的对象。

无法将类型“object”隐式转换为“GW2_tradingPost.Listings”。 存在显式转换(您是否缺少演员表?)第 32 行

我知道我做错了,但我不明白什么出了问题。

Form1.cs

private void button1_Click(object sender, EventArgs e)
    {
        Listings Listings = new Listings();
        api_Request api_Request = new api_Request();

        Listings richTextBox1 = api_Request.GetApi("https://api.guildwars2.com/v2/commerce/listings/19684");
    }

api_Request.cs

public class api_Request
    {
        public Object GetApi(string url)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            try
            {
                WebResponse response = request.GetResponse();
                using (Stream responseStream = response.GetResponseStream())
                {
                    //StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                    //return reader.ReadToEnd();
                    StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                    var jsonReader = new JsonTextReader(reader);
                    var serializer = new JsonSerializer();
                    return serializer.Deserialize<Listings>(jsonReader);
                }
            }
            catch (WebException ex)
            {
                WebResponse errorResponse = ex.Response;
                using (Stream responseStream = errorResponse.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
                    String errorText = reader.ReadToEnd();
                    // log errorText
                }
                throw;
            }
        }

    }

    public class Listings
    {
        [JsonProperty(PropertyName = "id")]
        public int Id { get; set; }

        public List<Buy> Buys { get; private set; }

        public Listings()
        {
            Buys = new List<Buy>();
        }
    }
    public class Buy
    {
        [JsonProperty(PropertyName = "listings")]
        public int Listings { get; set; }

        [JsonProperty(PropertyName = "unit_price")]
        public int UnitPrice { get; set; }

        [JsonProperty(PropertyName = "quantity")]
        public int Quantity { get; set; }
    }

我现在使用它来调用对象并在消息框中返回一个值,但它返回的 0 应该是 19684。

api_Request.GetApi("https://api.guildwars2.com/v2/commerce/listings/19684"); MessageBox.Show(Listings.Id.ToString());

【问题讨论】:

    标签: c# json object serialization


    【解决方案1】:

    您的签名是Object GetApi(string url)。将其更改为 Listings GetApi(string url),因为这是您从该方法实际返回的内容。

    还可以考虑给“GetApi”方法一个更合理的名称,以说明它实际上做了什么。

    【讨论】:

    • 好吧,实际上返回正确的类型很有意义。但是如何。但是为了访问对象,我该怎么做呢?现在它返回类名Listings,因为我没有说我想从对象中检索什么。
    • @Krijn 我不确定你的意思,但是这个类有你可以访问的属性。
    • 是的,我正要回复说我已经成功了,抱歉!只是一些愚蠢的语法,我一直在搞砸。非常感谢您帮助我!
    猜你喜欢
    • 2019-08-28
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    相关资源
    最近更新 更多