2017-06-29 18 views
1

我有以下響應我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;   
} 
+0

駕駛室編輯你的問題,顯示的是什麼_「所有的價值是類空」的一個例子_是,如何你」重新創建json? – stuartd

+0

@stuartd我添加了更多的代碼,請檢查它。 –

回答

1

我已經在ConsoleApplication中編寫了這段代碼,但它應該可以工作。如果你有對象的實例,你可以很容易檢查它是這樣的:

 public class JsonArray 
     { 
      public string param1 { get; set; } 
      public string param2 { get; set; } 
      public string param3 { get; set; } 
      public string param4 { get; set; } 
     } 

     public class MyClass 
     { 
      public List<JsonArray> json_array { get; set; } 
      public bool status { get; set; } 
     } 


     public static void Main() 
     { 
      // init 
      var myClassObj = new MyClass(); 

      myClassObj.json_array = new List<JsonArray>(); 
      myClassObj.json_array.Add(new JsonArray { param1 = null, param2 = null, param3 = null, param4 = null }); 
      myClassObj.json_array.Add(new JsonArray { param1 = null, param2 = null, param3 = null, param4 = null }); 
      myClassObj.status = true; 

      // to JSON 
      var json = FilterAndGetJsonFromObject(myClassObj); 
     } 

     public static string FilterAndGetJsonFromObject(MyClass myClassObj) 
     { 
      // filter null params 
      foreach (var jsonArray in myClassObj.json_array.ToList()) 
      { 
       if (jsonArray.param1 == null && jsonArray.param2 == null && jsonArray.param3 == null && jsonArray.param4 == null) 
       { 
        myClassObj.json_array.Remove(jsonArray); 
       } 
      } 

      // serialize to json 
      return new JavaScriptSerializer().Serialize(myClassObj); 
     } 
+1

謝謝。現在工作完美 –

0

我認爲你是什麼這樣的事情後:

List<MyClass> items = new List<MyClass>(); 

    for (int i = 0; i < ICOARPOST.Length; i++) 
    { 
     MyClass s = await _Services.Method(ICOARPOST[i].param1, ICOARPOST[i].param2); 

     //no idea if this is how it works. might as well be s.Value != null or something similar 
     if(s != null) 
      items.add(s); 
    } 

    responcearrayPost = items.ToArray(); 

但說實話,你的榜樣還遠遠沒有完成,所以我不能說這是否有什麼好處。

+0

當我返回類時,類參數爲null –

+0

我添加了更多的代碼,請檢查它。 –

-1

你正在使用循環來執行方法,它會得到sMyClass的哪個類對象作爲響應。 你檢查值如果爲空或空,然後限制分配在對象 responcearrayPost [i] = s;

if(s !=null) 
responcearrayPost[i] = s; 
+0

再仔細看看你的代碼。這樣,儘管有空檢查,你仍然會在數組中得到空值。 –

1

爲什麼不使用List<T>

var resultList = List<MyClass>(); 

for (int i = 0; i < ICOARPOST.Length; i++) 
{ 
    MyClass s = await _Services.Method(ICOARPOST[i].param1, ICOARPOST[i].param2); 

    if(s != null) 
    { 
     resultList.Add(s); 
    } 
} 
+1

我添加了更多的代碼,請檢查它。 –

+0

謝謝。我用列表現在它的工作 –