我正在学习C#,并制作了一个控制台应用程序,该应用程序读取文本条目并搜索返回JSON值的API,然后通过控制台显示这些值。 现在,我需要将其传递给图形环境,然后完成的工作是创建一个类“ MyClass”,并在该类中创建了一个方法“ MyMethod”,该方法将接收一个15位数字作为参数,并将查询API(例如控制台应用程序所做的操作),然后返回这些值。
我不知道如何正确创建方法以及如何从另一个类实例化该方法,因为它会产生错误。
我的JSON:
{
"success": true,
"result": {
"id": "20429683581",
"name": "CINEPLEX S.A",
"Condition": "HABIDO",
"PLE": "02\/01\/2013",
"lawyers": [
{
"type": "DNI",
"numdoc": "07871885",
"name": "PONCE PINTO ALEJANDRO EDUARDO",
"date": "22\/08\/2000"
},
{
"type": "DNI",
"numdoc": "09333203",
"name": "SORIANO BARRANTES JOSE FERNANDO",
"date": "22\/09\/2008"
}
],
"workers": [
{
"time": "2017-07",
"service": "8"
},
{
"time": "2018-06",
"service": "13"
}
]
}
}
我有以下已定义的类:
public class Lawyer
{
public string type { get; set; }
public string numdoc { get; set; }
public string name { get; set; }
public string date { get; set; }
}
public class Worker
{
public string time { get; set; }
public string service { get; set; }
}
public class Result
{
public string id { get; set; }
public string name { get; set; }
public string Condition { get; set; }
public string PLE { get; set; }
public List<Lawyer> lawyers { get; set; }
public List<Worker> workers { get; set; }
}
public class RootObject
{
public bool success { get; set; }
public Result result { get; set; }
}
在控制台应用程序中,它们运行良好:
RootObject rootobject;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://www.example.com/api/?get=" + inid);
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
{
var json = reader.ReadToEnd();
rootobject = JsonConvert.DeserializeObject<RootObject>(json);
}
if (rootobject.success == true)
{
Console.WriteLine("---------------");
Console.WriteLine("ID: " + rootobject.result.id);
Console.WriteLine("NAME: " + rootobject.result.name);
Console.WriteLine("CONDITION: " + rootobject.result.Condition);
Console.WriteLine("PLE: " + rootobject.result.PLE);
}
else
{
Console.WriteLine("---------------");
Console.WriteLine("NOTHING");
}
}
catch (Exception)
{
Console.WriteLine("---------------");
Console.WriteLine("ERROR");
}
此后,我已经使用MyClass
迁移到基于GUI的应用程序:
class MyClass
{
public void MyMethod(int inRUC){
RootObject rootobject;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://www.example.com/api/?get=" + inRUC);
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
{
var json = reader.ReadToEnd();
rootobject = JsonConvert.DeserializeObject<RootObject>(json);
}
}
}
现在,我想获取图形形式的文本条目的值,并实例化该类以获取结果并将其打印在相同形式的文本字段中,但是我不知道如何正确地实例化它以及如何进行实例化。为了正确放置值,这是我当前正在尝试的:(txtRuc1)是我对表单的输入,而EditText1是我对表单的输出:
private void Button1_ClickBefore(object sboObject, SAPbouiCOM.SBOItemEventArg pVal, out bool BubbleEvent)
{
if (string.IsNullOrEmpty(txtRuc1.Value.ToString()))
{
BubbleEvent = false;
}
else
{
//Ok, I known that it is wrong
BubbleEvent = true;
MyClass obj = new Sunat();
EditText1.Value = obj.MyMethod(txtRuc1.value).rootobject.result.name;
}
}
正确的格式是什么? Visual Studio返回错误:无法在以下行中将“字符串”转换为“ int”:
obj.MyMethod(txtRuc1.value).rootobject.result.name
答案 0 :(得分:2)
首先,您的MyMethod
需要返回RootObject
而不是void
public RootObject MyMethod(int inRUC){
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://www.example.com/api/?get=" + inRUC);
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
{
var json = reader.ReadToEnd();
return JsonConvert.DeserializeObject<RootObject>(json);
}
}
然后错误是由txtRuc1.value
引起的,可能是字符串值,但是MyMethod
方法需要int
。
因此您可以尝试使用int.TryParse
方法将输入值解析为int
,并通过它。
int para = 0;
int.TryParse(txtRuc1.value,out para);
EditText1.Value = obj.MyMethod(para).rootobject.result.name;