【发布时间】:2017-11-17 07:46:37
【问题描述】:
我有一个以字符串格式返回 json 数组 (JArray) 的 Web 服务,但我不明白如何将状态值添加到该操作并在使用该服务的应用程序中获取它。
我的问题是,我应该在 json 数组中返回一个带有消息的 json 对象吗?还是只是一个数组?哪个更方便?
我的生活:
public string getList(string strSalary)
{
List<Employee> listJson = null;
JObject jsonResp = "";
JArray array = null;
try
{
listJson = ReportBLL.getInstance.listEmployees(int.Parse(strSalary));
array = JArray.FromObject(listJson);
//set array status ?: ej
//array status = "succes";
}
catch (Exception ex)
{
//set error message
// array status = "error";
//array message = ex.Message or "Not found employees";
}
return JsonConvert.SerializeObject(array);
}
客户端调用(其他 asp 应用):
public static List<Employee> listEmployeeClient(string salary)
{
JObject jo = null; //or arrayjson ?
string strData = "";
strData = webService.getList(salary);
jo = JObject.Parse(strData);
//how to evalue status request ?
/* example
if(jo.status == "error") {
throw new Exception(jo.message);
} else {
iterate array inside json object ?
}
*/
}
这个逻辑正确吗?
【问题讨论】:
标签: c# .net json return response