kr_v
kr_v

Reputation: 139

Changing JSON return object names

I have this function for returning users and user count

List<User> entitiesList = DB.Users.OrderBy(x => x.UserName).ToList();
var count = DB.Users.Count
return (entitiesList, count)

The result from HTTP request is this

{
    "item1":
[
    {
        "fullName":"Joe Smith"
    },
    {"fullName":"Bob Rogan"
    }
]
    "item2":["2"]
}

Question is why does JSON result has objects named item1 and item2, where does these names come from, I haven't specified such names anywhere, shouldn't there just be no name in such case? And how do I rename 'item1' to 'users' and 'item2' to 'count'?

Upvotes: 0

Views: 155

Answers (1)

Bluesight
Bluesight

Reputation: 754

It's because you return a tuple, in your case (Class User, integer). You should return a custom class:

public class Response {
    public List<User> Users;
    public int Count;
}

....

return (new Response { Users = entitiesList, Count = count}); 

Upvotes: 1

Related Questions