2016-08-23 74 views
1

如何將序列化的json數據轉換爲camelCase數據而不是所有屬性的大寫?如何在客戶端將Json數據設置爲camelCase

在這裏我的看法是如何,我打開C#以JSON

team = @Html.Raw(Json.Encode(Model.Employees)); 

下面是數據: 「所有這一切都是測試數據的方式」

team = [ 
    { 
     "UserId": "902c2fe0-fcdf-4ed8-9591-5d8e01300cda", 
     "UserName": "buderbits", 
     "ID": 719, 
     "Email": "[email protected]", 
     "FName": "Builder", 
     "LName": "Bits", 
     "Title": "Glaizer", 
     "Zip": "10027", 
     "Phone": "456-768-9987", 
     "AddressLine": "124 street st.", 
     "City": "Nyc", 
     "StateCD": "NY", 
     "Avatar": "https://stackoverflow.com/users/RFyEQxDCpEavRfgDA028/qvzlezay.u1l.png", 
     "RowNum": 3, 
     "Total": 4, 
     "IsDirty": false, 
     "IsFlaggedForDelete": false, 
     "SortOrder": 0 
    }, 
    { 
     "UserId": "a9cec535-ac48-4197-b8a0-87702cb14e52", 
     "UserName": "stdard", 
     "ID": 717, 
     "Email": "[email protected]", 
     "FName": "Erik", 
     "LName": "Little", 
     "Title": "Developer", 
     "Zip": "75040", 
     "Phone": "123-456-7890", 
     "AddressLine": "123 Test Street", 
     "City": "Garland", 
     "StateCD": "TX", 
     "Avatar": "https://stackoverflow.com/users/CZmP9uvdTCg8CdEhOAX0/kcm1yg2o.spn.gif", 
     "RowNum": 1, 
     "Total": 4, 
     "IsDirty": false, 
     "IsFlaggedForDelete": false, 
     "SortOrder": 0 
    }, 
    { 
     "UserId": "1a4ef17a-9cd8-42e4-b172-3071e29a222a", 
     "UserName": "teammber1", 
     "ID": 2744, 
     "Email": "[email protected]", 
     "FName": "Derick", 
     "LName": "Mavrick", 
     "Title": "Drafting", 
     "Zip": "77702", 
     "Phone": "111-111-1111", 
     "AddressLine": "123 Teammember ST", 
     "City": "Beaumont", 
     "StateCD": "TX", 
     "Avatar": "https://stackoverflow.com/users/rPF9z0a7yLJpp4e1Zb15/os1cmk0r.kkg.jpg", 
     "RowNum": 4, 
     "Total": 4, 
     "IsDirty": false, 
     "IsFlaggedForDelete": false, 
     "SortOrder": 0 
    }, 
    { 
     "UserId": "17fab37f-d978-441c-a30d-8c08a7e6ce9b", 
     "UserName": "stoontsoftware", 
     "ID": 718, 
     "Email": "[email protected]", 
     "FName": "Mark", 
     "LName": "Taylor", 
     "Title": "Hosting", 
     "Zip": "75070", 
     "Phone": "345-432-1153", 
     "AddressLine": "123 street dr", 
     "City": "McKinney", 
     "StateCD": "TX", 
     "Avatar": "https://stackoverflow.com/users/y5TPPr0HR1GQLxkEhDuj/3bmwy0sk.qoi.png", 
     "RowNum": 2, 
     "Total": 4, 
     "IsDirty": false, 
     "IsFlaggedForDelete": false, 
     "SortOrder": 0 
    } 
]; 

幫助?

回答

3

您可以使用Newtonsoft的json序列化程序(Json.net),它允許您在序列化對象時指定格式化程序。

var d = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.Employees, 
     new JsonSerializerSettings 
     { 
      ContractResolver = new CamelCasePropertyNamesContractResolver() 
     })); 
console.log(d); 

這會給你通過序列化創建了JS對象/數組小寫屬性名稱。

+1

非常感謝! –

0

在發送給客戶端之前,您可以通過將您的初始查詢轉換爲動態對象或使用駱駝式字段的視圖模型,將您的字段轉換爲駱駝案例。例如,如果原始查詢:

var model=db.Records.ToList(); 

產生的輸出,然後修改您的查詢是:

var model=db.Records.Select(r=> 
    new {firstName=r.FirstName,lastName=LastName, etc...).ToList(); 

要真正做到這一點在客戶端100%,你將需要重建表對象使用一個javascript方法,用正確命名的對象替換每個對象。

function NewObject(input){ 
    input=input||{}; 
    this.firstName=input.FirstName||''; 
    this.lastName=input.LastName||''; 
    ...etc. 
} 

var [email protected]; //your Json data as an object 
for(var i=0;i<model.length;i++){ 
    model[i]=new NewObject(model[i]); 
} 
相關問題