我在JsonArray webapi中获得了null值#

时间:2017-06-29 10:47:08

标签: c# asp.net asp.net-web-api

我收到了以下回复

{
    "json_array": 
    [
        {
            "param1": null,
            "param2": null,
            "param3": null,
            "param4": null
        },
        {
            "param1": null,
            "param2": null,
            "param3": null,
            "param4": null
        }
   ],
   "status": "true"
}
  

如果类中的所有值都为null,我想要这种类型的响应。

{
    "json_array": [],
    "status": "true"
}

AND 如果不为null,我会传递此类型的响应

{
"json_array": 
[
    {
        "param1": "123",
        "param2": "true",
        "param3": "success",
        "param4": "1"
    },
    {
        "param1": "129",
        "param2": "true",
        "param3": "success",
        "param4": "2"
    }
 ],
 "status": "true" }

我返回了类的对象,我在这里循环传递

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;        
}

4 个答案:

答案 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 :(得分: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);
        }

答案 2 :(得分: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();

但说实话,你的榜样远非完整,所以我无法判断这对你有好处。

答案 3 :(得分:-1)

你正在使用循环来执行方法,它会获得sMyClass的哪个类对象作为响应。 如果为null或为空,则检查值,然后限制在对象中赋值 responcearrayPost [i] = s;

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