我有以下響應我JsonArray的WebAPI了空值c#
{
"json_array":
[
{
"param1": null,
"param2": null,
"param3": null,
"param4": null
},
{
"param1": null,
"param2": null,
"param3": null,
"param4": null
}
],
"status": "true"
}
我想這種反應如果所有的值是類空。
{
"json_array": [],
"status": "true"
}
AND我通過這種類型的響應,如果不爲空
{
"json_array":
[
{
"param1": "123",
"param2": "true",
"param3": "success",
"param4": "1"
},
{
"param1": "129",
"param2": "true",
"param3": "success",
"param4": "2"
}
],
"status": "true" }
我返回類對象和我通過像這樣在for循環
public async Task<IHttpActionResult> MethodCheckin(RequestClass CR)
{
RequestClass[] ICOARPOST = CR.postobject;
MyClass[] responcearrayPost = new MyClass[ICOARPOST.Length];
Response_Class _CR = new Response_Class();
if (ICOARPOST == null || ICOARPOST.Length == 0)
{
_CR.status = Constant.False;
_CR.datapost = responcearrayPost;
return Ok(_CR);
}
for (int i = 0; i < ICOARPOST.Length; i++)
{
MyClass s = await _Services.Method(ICOARPOST[i].param1, ICOARPOST[i].param2);
responcearrayPost[i] = s;
}
_CR.status = Constant.True;
_CR.datapost = responcearrayPost;
return Ok(_CR);
}
這是我的班級,
public class MyClass
{
public string param1 { get; set; }
public string param2 { get; set; }
public string param3 { get; set; }
public string param4 { get; set; }
}
這裏是我在課堂返回值函數,
public async Task<MyClass> AsyncDay(string param1, string param2)
{
MyClass _myclass = new MyClass();
if (Time >= 86400000 && Time < 172800000)
{
if (day == 1)
{
}
else
{
_myclass.param1 = "123";
_myclass.param2 = "1";
_myclass.param3 = "success";
_myclass.param4 = "true";
}
}
else if (Time >= 172800000 && Time < 259200000)
{
if (day == 2)
{
}
else
{
_myclass.param1 = "123";
_myclass.param2 = "1";
_myclass.param3 = "success";
_myclass.param4 = "true";
}
}
return _myclass;
}
駕駛室編輯你的問題,顯示的是什麼_「所有的價值是類空」的一個例子_是,如何你」重新創建json? – stuartd
@stuartd我添加了更多的代碼,請檢查它。 –