Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从Json序列化中忽略类属性_C#_Asp.net_Json_Web Services - Fatal编程技术网

C# 如何从Json序列化中忽略类属性

C# 如何从Json序列化中忽略类属性,c#,asp.net,json,web-services,C#,Asp.net,Json,Web Services,我正在使用asp.net 4.0创建一个Web服务 我已经创建了一个asmx文件并创建了一个User.cs类。它有8个属性。 我返回了一个json格式的服务。 如果userlogin为true,我需要返回user.cs的所有属性,如果为fail,我只需要返回2个属性 如何实现它 用户登录为true。它将返回所有 {“消息”:“有效用户”,“BranchId”:1,“BranchName”:“我的分支名称”,“Id”:1,“名称”:“管理员”,“密码”:“管理员”,“角色Id”:1,“状态”:1}

我正在使用asp.net 4.0创建一个Web服务

我已经创建了一个asmx文件并创建了一个User.cs类。它有8个属性。 我返回了一个json格式的服务。 如果userlogin为true,我需要返回user.cs的所有属性,如果为fail,我只需要返回2个属性

如何实现它

用户登录为true。它将返回所有

{“消息”:“有效用户”,“BranchId”:1,“BranchName”:“我的分支名称”,“Id”:1,“名称”:“管理员”,“密码”:“管理员”,“角色Id”:1,“状态”:1}

用户登录失败,我只需重新运行消息和状态。但它会像foloows一样返回

{message”:“用户名或密码无效”,“BranchHid”:0,“BranchName”:null,“Id”:0,“Name”:null,“password”:null,“RoleId”:0,“Status”:0}

我有谷歌它,并得到以下。如何根据我的登录状态使用它

如果我在属性中使用了[ScriptIgnore],则在这两种情况下它都会忽略属性。登录失败时,我需要忽略属性

我的财产是这样的

// Properties
    public int BranchId
    {
        get { return _BranchId; }
        set { if (_BranchId != value) { _BranchId = value; } }
    }

    public string BranchName
    {
        get { return _BranchName; }
        set { _BranchName = value; }
    }

    private String _message;

    public String message
    {
        get { return _message; }
        set { _message = value; }
    }
我的网络服务

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void appLogin(String userName, String passWord)
    {
        Admin_AppUserBLL objusr = Admin_AppUserBLL.GetAdmin_AppUserBLL(userName);
        string strResponse = "";
        if (objusr != null)
        {
            if (objusr.Password == passWord)
            {
                objusr.message = "valid username";
                strResponse = new JavaScriptSerializer().Serialize(objusr);
            }
        }
        else
        {
            objusr = new Admin_AppUserBLL();
            objusr.message = "username or password is invalid";
            strResponse = new JavaScriptSerializer().Serialize(objusr);
        }
        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        Context.Response.AddHeader("content-length", strResponse.Length.ToString());
        Context.Response.Flush();
        Context.Response.Write(strResponse);
    }

在属性上添加以下属性,并使用“”使其可为空


如果值为null,并且json不包含这些peoperties,则它将忽略。

在Newtonsoft中添加引用

using Newtonsoft.Json;
序列化对象时

string strResponse = JsonConvert.SerializeObject(objusr, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

它将避免空值属性

如果显示以下错误,则无法找到类型或命名空间名称“JsonProperty”(您是否缺少using指令或程序集引用?请在应用程序中添加对NewtonSoft的引用。如果问题得到解决,请标记为“解决”。@Hisanth使用的是JavaScriptSerializer,而不是NewtonSoft。它是Json.net的一部分。请查看以下链接。)-可能重复
string strResponse = JsonConvert.SerializeObject(objusr, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });