【发布时间】:2020-05-16 18:28:18
【问题描述】:
API 响应:
{
"success": true,
"code": 0,
"msg": "성공하였습니다.",
"list": [
{
"code": "ANT",
"code_name": "소둔구분",
"p_code": "",
"p_code_name": "",
"code_type": 0,
"code_level": 0,
"max_level": 1,
"description": "GROP : 99 DETL : ANT SUBS : 00",
"create_user": "SYSTEM",
"create_time": "2019-04-24T17:58:58.000+0000",
"disable_yn": "Y"
},
{...}
]
}
C# API 调用:
// CodeInfo 초기 데이터 로드
public static DataTable selectAllCodeInfo()
{
DataTable dt = new DataTable();
try
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(API_ADDRESS + "/api/codeInfo");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
JObject applyObj = JObject.Parse(streamReader.ReadToEnd());
var applyObj2 = streamReader.ReadToEnd();
string success = applyObj["success"].ToString();
if (success.Equals("True"))
{
if (applyObj["list"].ToString() != null)
{
??????????
return dt;
}
} else
{
//API 응답 데이터 수신 실패
}
}
} catch(WebException)
{
//API 서버 닫혀있을때, 연결이 안될때
Console.Write("예외");
return null;
}
catch (Exception)
{
//그 외의 Exception
Console.Write("예외");
return null;
}
return dt;
}
我想有效地将上述 API 调用返回数据值加载到 DataTable 中。
我想了几种方法,但还没有找到有效的方法。
我尝试使用 json 数据作为属性对象。 但是太麻烦了,效率低。
在上面的Json数据中,我只想将列表数组中包含的数据加载为DataTable中的列和行。
有什么好的选择吗?
【问题讨论】:
标签: c#