Akhil George
Akhil George

Reputation: 17

Modifications in json format

This is my api in json format

[{"tu_id":1,"tu_name":"Akhil","tu_usrname":"Akhl","tu_pwd":"1234","tu_cntno":"423433"}]

I want ID instead of tu_id and Name instead of tu_name

this is my code in controller

public IEnumerable<TestUser> Get()
    {
        DataTable dt = new DataTable();
        TestUserBL userbl = new TestUserBL();
        dt = userbl.TestUserSel();
        var user = dt.AsEnumerable().Select(x => new TestUser
        {


              tu_id = x.Field<int>("tu_id"),
              tu_name = x.Field<string>("tu_name"),
              tu_usrname=x.Field<string>("tu_usrname"),
              tu_pwd=x.Field<string>("tu_pwd"),
              tu_cntno=x.Field<string>("tu_cntno")
            // like wise initialize properties here
        });

        return user;
    }

Here I cant change tu_id?

Upvotes: 1

Views: 45

Answers (2)

jitender
jitender

Reputation: 10429

Change property names(tu_id to ID and tu_name to Name etc) in TestUser class they don't follow the conventions conventions then do mapping as follow:-

 var user = dt.AsEnumerable().Select(x => new TestUser
        {

              Id= x.Field<int>("tu_id"),
              Name = x.Field<string>("tu_name")
        });

Upvotes: 1

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

You can use serialization attributes applied to properties of your TestUser class:

 [JsonProperty("ID")]
 public int tu_id { get; set; }

 [JsonProperty("Name")]
 public string tu_name { get; set; }

Upvotes: 1

Related Questions