2

我不知道如何描述这一点,但我会尽我所能。我有一个 C# 应用程序,它采用我的 Web 应用程序的 API 并将它的 JSON 响应用作应用程序的数据。当用户单击按钮时,它会从 url 中提取响应并对其进行解析,以便可以使用它:

        var client = new WebClient();
        client.Headers.Add("User-Agent", "Nobody");
        var response = client.DownloadString(new Uri("http://localhost:45035/api/products/1"));
        var responsestring = response.ToString();
        JObject o = JObject.Parse(responsestring);

        Int32 Id = (int)o["Id"];
        string Name = (string)o["Name"];
        string Category = (string)o["Category"];
        float Price = (float)o["Price"];
        string Website = (string)o["Website"];

        label1.Text = Name;
        label2.Text = "$" + Price.ToString();
        label3.Text = "Category: " + Category;
        label4.Text = Id.ToString();

这很好用。问题是,当我有数千种产品时,这个应用程序将有上千个代码块,就像这样,只更改了 DownloadString Uri。我想把它变成一个类,这样我就可以插入适当的 Uri(例如http://example.com:45035/api/products/1http://example.com:45035/api/products/2等)并从中获取名称、类别、ID 等,这样我的代码会更干净,但我不知道该怎么做。

我试过类似的东西:

        public static object responseinfo(string url)
    {
        var client = new WebClient();
        client.Headers.Add("User-Agent", "Nobody");
        var response = client.DownloadString(new Uri(url));
        var responsestring = response.ToString();
        JObject o = JObject.Parse(responsestring);

        Int32 Id = (int)o["Id"];
        string Name = (string)o["Name"];
        string Category = (string)o["Category"];
        float Price = (float)o["Price"];
        string Website = (string)o["Website"];
    }

这允许我调用: jsonfind("http://localhost:45035/api/products/1") 但我不知道如何取出字符串,所以我可以像以前一样在文本框中使用它们。

我希望这种制作自始至终。这是我的第一个问题,所以如果我需要更改一些或很多,请告诉我。

作为记录,我正在使用 Json.NET 处理 JSON 响应。

谢谢,

米切尔

4

4 回答 4

4

解决您的问题的最直接方法是使用out参数

public void GetTwoNumers(out int num1, out int num2) {
    num1 = 4;
    num2 = 2;
}

更好的解决方案是将WebClient代码提取到一个单独的函数中,该函数返回 a JObject

public static JObject WebRequest(string url) {
    var client = new WebClient();
    client.Headers.Add("User-Agent", "Nobody");
    var response = client.DownloadString(new Uri(url));
    var responsestring = response.ToString();
    return JObject.Parse(responsestring);
}

然后,在许多其他 API 调用函数中使用该函数,每个函数都返回自己的类和相关字段:

public class Item {
    public int Id { get; private set; }
    public string Name { get; private set; }
    public string Category { get; private set; }
    public float Price { get; private set; }
    public string Website { get; private set; }

    private Item() {
    }

    public static Item GetFromUrl(string url) {
       var o = WebRequest(url);

        return new Item() {
            Id = (int)o["Id"],
            Name = (string)o["Name"],
            Category = (string)o["Category"],
            Price = (float)o["Price"],
            Website = (string)o["Website"],
        };
    }
}

然后调用这段代码:

private void button1_Click(object sender, EventArgs e) {
    string url = "...";
    var item = Item.GetFromUrl(url);

    MessageBox.Show("You found item #" + item.Id + " named " + item.Name);

    txtBoxName.Text = item.Name;
    txtBoxCat.Text = item.Category;
}

注意我在这里使用了静态工厂方法GetFromUrl,并将构造函数设为私有。因此,您只能通过该静态方法获取实例。不是完全必要的,但这里是一个很好的技术,IMO。

于 2012-07-26T04:48:32.143 回答
1

我能想到几个选择:

1)创建自己的类型并返回它:

class JObjectReturned {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
    public float Price { get; set; }
    public string Website { get; set; }
}

public static JObjectReturned responseinfo(string url)
{
    var client = new WebClient();
    client.Headers.Add("User-Agent", "Nobody");
    var response = client.DownloadString(new Uri(url));
    var responsestring = response.ToString();
    JObject o = JObject.Parse(responsestring);

    return new JObjectReturned() { 
        Id = (int)o["Id"],
        Name = (string)o["Name"],
        Category = (string)o["Category"],
        Price = (float)o["Price"],
        Website = (string)o["Website"]
    };
}

然后你可以像这样使用它:

for (int i = 0; i < urls.Length; i++) { 
    JObjectReturned obj = responseInfo(urls[i]);
    // obj.Name, obj.Price, etc..
}

2) 返回 JObject。

.. 只是不要返回元组。

于 2012-07-26T04:51:47.470 回答
0

你有没有想过使用一个类,让一个产品,来存储和检索值?

puclic class Product
{
   int ID;
   string Name;
   string Category;
   string Website;
   float Price;
}

如果你真的想的话,你甚至可以使用DataTable 。

于 2012-07-26T04:51:53.363 回答
0

也许是这样的:

public class ProductDTO
{
    public ProductDTO(string SourceURI)
    {
        this.SourceURI = SourceURI;
    }

    public void GetReady()
    {
        var client = new WebClient();
        client.Headers.Add("User-Agent", "Nobody");
        var response = client.DownloadString(new Uri(this.SourceURI));
        var responsestring = response.ToString();
        JObject o = JObject.Parse(responsestring);

        this.Id = (int)o["Id"];
        this.Name = (string)o["Name"];
        this.Category = (string)o["Category"];
        this.Price = (float)o["Price"];
        this.Website = (string)o["Website"];
    }

    public int Id { get; private set; }
    public string Name { get; private set; }
    public string Category { get; private set; }
    public float Price { get; private set; }
    public string Website { get; private set; }
    public string SourceURI { get; private set; }
}

问候

于 2012-07-26T04:54:25.263 回答