从 C# 中的 JsonResult 中删除一个元素
Remove an element from JsonResult in C#
我有一个从 MVC 方法到 return 的 JsonResult
对象,但我需要在发送它之前从中删除一个元素。
更新:
我试图在不映射它的情况下进行操作,因为该对象非常庞大且非常复杂。
我怎样才能做到这一点?
例如:
public class MyClass {
public string PropertyToExpose {get; set;}
public string PropertyToNOTExpose {get; set;}
public string Otherthings {get; set;}
}
和
JsonResult result = new JsonResult();
result = Json(myObject, JsonRequestBehavior.AllowGet);
然后 从结果中删除 PropertyToNOTExpose。
从实际代码更新:
public JsonResult GetTransaction(string id)
{
//FILL UP transaction Object
JsonResult resultado = new JsonResult();
if (CONDITION USER HAS NOT ROLE) {
var jObject = JObject.FromObject(transaction);
jObject.Remove("ValidatorTransactionId");
jObject.Remove("Validator");
jObject.Remove("WebSvcMethod");
resultado = Json(jObject, JsonRequestBehavior.AllowGet);
} else {
//etc.
}
return resultado;
}
您可以创建一个新对象,排除您不想在结果中发送的属性...
var anonymousObj = new {
myObject.PropertyToExpose,
myObject.Otherthings
};
JsonResult result = Json(anonymousObj, JsonRequestBehavior.AllowGet);
另一种选择是将对象转换为 Newtonsoft.Json.Linq.JObject and remove the property using JObject.Remove Method (String)
var jObject = JObject.FromObject(myObject);
jObject.Remove("PropertyToNOTExpose");
var json = jObject.ToString(); // Returns the indented JSON for this token.
var result = Content(json,"application/json");
您可以尝试在 属性 上使用 [ScriptIgnore]
属性。这将导致 JavaScriptSerializer
忽略它。但是,这意味着它在反序列化时也会被忽略。我不确定这是否适合您的情况。
public class myClass
{
public string PropertyToExpose {get; set;}
[ScriptIgnore]
public string PropertyToNOTExpose {get; set;}
public string Otherthings {get; set;}
}
我有一个从 MVC 方法到 return 的 JsonResult
对象,但我需要在发送它之前从中删除一个元素。
更新:
我试图在不映射它的情况下进行操作,因为该对象非常庞大且非常复杂。
我怎样才能做到这一点?
例如:
public class MyClass {
public string PropertyToExpose {get; set;}
public string PropertyToNOTExpose {get; set;}
public string Otherthings {get; set;}
}
和
JsonResult result = new JsonResult();
result = Json(myObject, JsonRequestBehavior.AllowGet);
然后 从结果中删除 PropertyToNOTExpose。
从实际代码更新:
public JsonResult GetTransaction(string id)
{
//FILL UP transaction Object
JsonResult resultado = new JsonResult();
if (CONDITION USER HAS NOT ROLE) {
var jObject = JObject.FromObject(transaction);
jObject.Remove("ValidatorTransactionId");
jObject.Remove("Validator");
jObject.Remove("WebSvcMethod");
resultado = Json(jObject, JsonRequestBehavior.AllowGet);
} else {
//etc.
}
return resultado;
}
您可以创建一个新对象,排除您不想在结果中发送的属性...
var anonymousObj = new {
myObject.PropertyToExpose,
myObject.Otherthings
};
JsonResult result = Json(anonymousObj, JsonRequestBehavior.AllowGet);
另一种选择是将对象转换为 Newtonsoft.Json.Linq.JObject and remove the property using JObject.Remove Method (String)
var jObject = JObject.FromObject(myObject);
jObject.Remove("PropertyToNOTExpose");
var json = jObject.ToString(); // Returns the indented JSON for this token.
var result = Content(json,"application/json");
您可以尝试在 属性 上使用 [ScriptIgnore]
属性。这将导致 JavaScriptSerializer
忽略它。但是,这意味着它在反序列化时也会被忽略。我不确定这是否适合您的情况。
public class myClass
{
public string PropertyToExpose {get; set;}
[ScriptIgnore]
public string PropertyToNOTExpose {get; set;}
public string Otherthings {get; set;}
}