Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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
Asp.net JsonResult等效于[DataMember(Name=";Test";)]_Asp.net_Json_Asp.net Mvc 2_Jsonresult - Fatal编程技术网

Asp.net JsonResult等效于[DataMember(Name=";Test";)]

Asp.net JsonResult等效于[DataMember(Name=";Test";)],asp.net,json,asp.net-mvc-2,jsonresult,Asp.net,Json,Asp.net Mvc 2,Jsonresult,我有一个这样做的方法: public JsonResult Layar(string countryCode, string timestamp, string userId, string developerId, string layarName, double radius, double lat, double lon, double accuracy) { LayarModel

我有一个这样做的方法:

public JsonResult Layar(string countryCode, string timestamp, string userId, 
                        string developerId, string layarName, double radius, 
                        double lat, double lon, double accuracy)
{
    LayarModel model = new LayarModel(lat, lon, radius);

    return Json(model, JsonRequestBehavior.AllowGet);
}
它返回此对象:

public class LayarModel
{        
    private List<HotSpot> _hotSpots = new List<HotSpot>();
    public List<HotSpot> HotSpots { get { return _hotSpots; } set { _hotSpots = value; } }    
    public string Name { get; set; }    
    public int ErrorCode { get; set; }
    public string ErrorString { get; set; }
}
在返回的类中,所有内容都大写(
热点
,而不是
热点

我尝试过DataContract和DataMembers(Name=“Test”),但这不起作用。有什么建议吗?

JsonResult()在内部使用JavaScriptSerializer进行序列化,它似乎不支持使用属性定义序列化的属性名

DataContractJsonSerializer支持这一点,因此这可能是一种方法

一些可能有用的链接:

  • JavaScriptSerializer.Deserialize-如何更改字段名:
  • DataContractJsonSerializer与JavaScriptSerializer:更改字段名称:

我还建议安装json.NET,但其余的要简单得多。下面是我在当前应用程序中使用的一种扩展方法,以提供更好的重用,可以根据您的需要随意调整它,但它应该能够立即满足您的需要

public class JsonNetResult : ActionResult
{
    public Encoding ContentEncoding { get; set; }
    public string ContentType { get; set; }
    public object Data { get; set; }

    public JsonSerializerSettings SerializerSettings { get; set; }
    public Formatting Formatting { get; set; }

    public JsonNetResult()
    {
        SerializerSettings = new JsonSerializerSettings
            {
                //http://odetocode.com/blogs/scott/archive/2013/03/25/asp-net-webapi-tip-3-camelcasing-json.aspx
                #if DEBUG
                Formatting = Formatting.Indented, //Makes the outputted Json easier reading by a human, only needed in debug
                #endif
                ContractResolver = new CamelCasePropertyNamesContractResolver() //Makes the default for properties outputted by Json to use camelCaps
            };
    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");

        HttpResponseBase response = context.HttpContext.Response;

        response.ContentType = !string.IsNullOrEmpty(ContentType)
                                    ? ContentType
                                    : "application/json";

        if (ContentEncoding != null)
            response.ContentEncoding = ContentEncoding;

        if (Data != null)
        {
            JsonTextWriter writer = new JsonTextWriter(response.Output) {Formatting = Formatting};

            JsonSerializer serializer = JsonSerializer.Create(SerializerSettings);
            serializer.Serialize(writer, Data);

            writer.Flush();
        }
    }
}

public static class JsonNetExtenionMethods
{
    public static ActionResult JsonNet(this Controller controller, object data)
    {
        return new JsonNetResult() {Data = data};
    }

    public static ActionResult JsonNet(this Controller controller, object data, string contentType)
    {
        return new JsonNetResult() { Data = data, ContentType = contentType };
    }

    public static ActionResult JsonNet(this Controller controller, object data, Formatting formatting)
    {
        return new JsonNetResult() {Data = data, Formatting = formatting};
    }
}
下面是一个使用它的示例

public JsonNetResult Layar(string countryCode, string timestamp, string userId, 
                        string developerId, string layarName, double radius, 
                        double lat, double lon, double accuracy)
{
    LayarModel model = new LayarModel(lat, lon, radius);

    return this.JsonNet(model);
}
需要特别注意的是,当
JsonSerializerSettings
上的
ContractResolver
设置为使用
new CamelCasePropertyNamesContractResolver()

这样,您就不必再次设置自定义命名

public JsonNetResult Layar(string countryCode, string timestamp, string userId, 
                        string developerId, string layarName, double radius, 
                        double lat, double lon, double accuracy)
{
    LayarModel model = new LayarModel(lat, lon, radius);

    return this.JsonNet(model);
}