简体   繁体   中英

How do I return the json data from a multi table?

I am using C# web service + Linq to SQL Classes + Return JSON Data.

For example my table:

表

Data from this table return JSON:

JSON格式

I want to make this application using C#?

Create New Class for Combine Class:

public class BadgeVoleModel
{
    public List<Badge> BadgeList = new List<Badge>();

    public List<VoleType> VoleTypeList = new List<VoleType>();
}

Create Method (return string) for handle get data from DataContext and populate data to BadgeVoleModel, and call from Controller that method.

    public string GetData()
    {
        var data = new Models.BadgeVoleModel();

        data.BadgeList = db.Badges.ToList();
        data.VoleTypeList = db.VoleTypes.ToList();

        var result = Newtonsoft.Json.JsonConvert.SerializeObject(data);

        return result;
    }

And for Deserialize Data:

    public Models.BadgeVoleModel DeserializeData(string data)
    {
        var result = Newtonsoft.Json.JsonConvert
                    .DeserializeObject<Models.BadgeVoleModel>(data);

        return result;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM