简体   繁体   中英

Return only one value from JObject with two values in it C#

How can I return only one of those values in response of method not both of them.

        return new JObject
        {
            ["accepted"] = acceptedArr,
            ["declined"] = declinedArr
        };

I mean if I want to return only the value of acceptedArr ,not to display both of them like

         { "acceptedArr": 
            ["some info"], 
          "declinedArr": [] } 

You can conditionally populate the properties you want by first creating an empty jObject and then assigning the appropriate property based on some condition:

var jObject = new JObject();

if (accepted)
{
    jObject["accepted"] = acceptedArr;
}
else
{
    jObject["declined"] = declinedArr;
}

return jObject;

If the object was created already before your code starts, then you can remove the property that you don't want:

public JObject SomeMethod(JObject obj)
{
    if ( /* condition here */ )
    {
        obj.Remove("accepted");
    }
    else
    {
        obj.Remove("declined");
    }
    return obj;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM