加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 相当于[DataMember(Name =“Test”)]的JsonResult

发布时间:2020-12-16 09:34:07 所属栏目:asp.Net 来源:网络整理
导读:我有一个方法这样做: 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); ret
我有一个方法这样做:

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

我想要JSON

{"hotspots": [{
    "distance": 100,"attribution": "The Location of the Layar Office","title": "The Layar Office","lon": 4884339,"imageURL": http://custom.layar.nl/layarimage.jpeg,"line4": "1019DW Amsterdam","line3": "distance:%distance%","line2": "Rietlandpark 301","actions": [],"lat": 52374544,"type": 0,"id": "test_1"}],"layer": "snowy4","errorString": "ok","morePages": false,"errorCode": 0,"nextPageKey": null
}

一切都在返回的类中出现大写(HotSpots而不是热点).

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

解决方法

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

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

一些可能有用的链接:

> JavaScriptSerializer.Deserialize – 如何更改字段名称:JavaScriptSerializer.Deserialize – how to change field names
> DataContractJsonSerializer与JavaScriptSerializer:更改字段名称:http://publicityson.blogspot.com/2010/06/datacontractjsonserializer-versus.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读