具有多个返回字符串和int的C#类
我不知道该如何描述,但我会尽我所能。我有一个C#应用程序,它接受我的web应用程序的API,并使用它的JSON响应作为应用程序的数据。当用户单击按钮时,它会从url中提取响应并对其进行解析,以便使用:具有多个返回字符串和int的C#类,c#,C#,我不知道该如何描述,但我会尽我所能。我有一个C#应用程序,它接受我的web应用程序的API,并使用它的JSON响应作为应用程序的数据。当用户单击按钮时,它会从url中提取响应并对其进行解析,以便使用: var client = new WebClient(); client.Headers.Add("User-Agent", "Nobody"); var response = client.DownloadString(new Uri("http:
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();
这很有效。问题是,当我有数千个产品时,这个应用程序将有上千个这样的代码块,只有下载字符串Uri发生了变化。我想把它转换成一个类,这样我就可以插入适当的Uri(如or等),并从中获得名称、类别、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响应
谢谢
Mitchell回答您的问题最直接的方法是:
更好的解决方案是将
WebClient
代码拉出到一个单独的函数中,该函数返回一个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
,并将构造函数设置为私有。因此,您只能通过该静态方法获取实例。不完全是必要的,但这是一个很好的技巧,我可以想出几个选择:
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..
}
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; }
}
for(inti=0;i
2) 返回作业对象
。。只是不要返回元组。您是否考虑过使用类,让sa产品来存储和检索值
puclic class Product
{
int ID;
string Name;
string Category;
string Website;
float Price;
}
如果你真的想,你甚至可以使用一个
for (int i = 0; i < urls.Length; i++) {
JObjectReturned obj = responseInfo(urls[i]);
// obj.Name, obj.Price, etc..
}
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; }
}
问候Hi:坦白说,我不明白:(.Q:您的初始代码块,`var client=new WebClient()……`,住在一个C#类中,不是吗?Q:如果是这样,为什么你不直接创建该类的名称、类别、价格和网站成员?这些代码用于一个按钮:Button1单击。当按钮被单击时,第一个代码块被执行。好吧-那么为什么不创建Name等类成员?我想我明白你的意思。你能举一个关于ho的简短例子吗你会把函数分开吗?很抱歉问了这么多问题,但我很难理解我应该如何使用它。可能是因为我感到沮丧:(.假设用户单击一个按钮:Button1\u Click。该按钮将告诉类导航到哪里,然后我需要它返回Id、名称等,以便我可以将其放入文本框(例如textbox1.text=Name,textbox2.text=Category)这样做了吗?我就是看不到?谢谢你的帮助!添加了一个调用我们刚刚编写的
项.GetFromUrl
的示例。