你好反序列化嵌套的JSON到C#对象

时间:2018-08-24 01:23:13

标签: c# json gridview

"Data": [{
    "timeLive": "74",
    "halfTimeScore": "0 - 0",
    "fullTimeScore": null,
    "firstTeamToScore": null,
    "homeTeamInfo": {
        "homeTeam": "Atlético Mineiro",
        "homeGoals": "0",
        "homeGoalsHalfTime": "0",
        "homeCorners": "9",
        "homeYellowCards": "1",
        "homeRedCards": "0",
        "homeShotsGol": "3",
        "homeShotsFora": "9",
        "homeAttacks": "131",
        "homeDangerousAttack": "49",
        "homePossession": "74",
        "homeFouls": 13,
        "avgGoalsHome": 2.1,
        "teamID": "43669",
        "homeTeamForm": {
            "position": "6",
            "points": "33",
            "form": [
                "W",
                "D",
                "L",
                "W",
                "W"
            ]
        }
    },
    "awayTeamInfo": {
        "awayTeam": "Vasco da Gama",
        "awayGoals": "0",
        "awayGoalsHalfTime": "0",
        "awayCorners": "1",
        "awayYellowCards": "3",
        "awayRedCards": "0",
        "awayShotsGol": "3",
        "awayShotsFora": "4",
        "awayAttacks": "58",
        "awayDangerousAttack": "21",
        "awayPossession": "26",
        "awayFouls": 14,
        "avgGoalsAway": 0.9,
        "teamID": "43663",
        "awayTeamForm": {
            "position": "15",
            "points": "20",
            "form": [
                "W",
                "L",
                "L",
                "L",
                "D"
            ]
        }
    },
    "head2head": {
        "draws": 3,
        "homeWins": 6,
        "awayWins": 6,
        "avgGolsH2H": 2.3
    },
    "matchEvents": [{
            "eventName": "Card",
            "eventType": "Yellow card",
            "eventTeam": "Away",
            "eventTime": 72,
            "eventPlayer": {
                "playerName": "Martín Silva",
                "playerShortName": "M. Silva",
                "playerID": "547DD"
            },
            "eventSubIn": null,
            "eventSubOut": null,
            "eventHomeTeam": false
        }
]

我有课,但我不知道如何从json转换为gridview 有人可以帮忙一个例子

1 个答案:

答案 0 :(得分:0)

如果要在服务器端执行此操作,这是您需要的解决方案。

如您所说,您已经准备好class或可以使用http://json2csharp.com(从json生成c#类)为json创建class

public class HomeTeamForm
{
    public string position { get; set; }
    public string points { get; set; }
    public List<string> form { get; set; }
}

public class HomeTeamInfo
{
    public string homeTeam { get; set; }
    public string homeGoals { get; set; }
    public string homeGoalsHalfTime { get; set; }
    public string homeCorners { get; set; }
    public string homeYellowCards { get; set; }
    public string homeRedCards { get; set; }
    public string homeShotsGol { get; set; }
    public string homeShotsFora { get; set; }
    public string homeAttacks { get; set; }
    public string homeDangerousAttack { get; set; }
    public string homePossession { get; set; }
    public int homeFouls { get; set; }
    public double avgGoalsHome { get; set; }
    public string teamID { get; set; }
    public HomeTeamForm homeTeamForm { get; set; }
}

public class AwayTeamForm
{
    public string position { get; set; }
    public string points { get; set; }
    public List<string> form { get; set; }
}

public class AwayTeamInfo
{
    public string awayTeam { get; set; }
    public string awayGoals { get; set; }
    public string awayGoalsHalfTime { get; set; }
    public string awayCorners { get; set; }
    public string awayYellowCards { get; set; }
    public string awayRedCards { get; set; }
    public string awayShotsGol { get; set; }
    public string awayShotsFora { get; set; }
    public string awayAttacks { get; set; }
    public string awayDangerousAttack { get; set; }
    public string awayPossession { get; set; }
    public int awayFouls { get; set; }
    public double avgGoalsAway { get; set; }
    public string teamID { get; set; }
    public AwayTeamForm awayTeamForm { get; set; }
}

public class Head2head
{
    public int draws { get; set; }
    public int homeWins { get; set; }
    public int awayWins { get; set; }
    public double avgGolsH2H { get; set; }
}

public class EventPlayer
{
    public string playerName { get; set; }
    public string playerShortName { get; set; }
    public string playerID { get; set; }
}

public class MatchEvent
{
    public string eventName { get; set; }
    public string eventType { get; set; }
    public string eventTeam { get; set; }
    public int eventTime { get; set; }
    public EventPlayer eventPlayer { get; set; }
    public object eventSubIn { get; set; }
    public object eventSubOut { get; set; }
    public bool eventHomeTeam { get; set; }
}

public class RootObject : List<RootObject>
{
    public string timeLive { get; set; }
    public string halfTimeScore { get; set; }
    public object fullTimeScore { get; set; }
    public object firstTeamToScore { get; set; }
    public HomeTeamInfo homeTeamInfo { get; set; }
    public AwayTeamInfo awayTeamInfo { get; set; }
    public Head2head head2head { get; set; }
    public List<MatchEvent> matchEvents { get; set; }
}

现在,您需要将Deserialize json添加到该类中。

RootObject myCShartData = new JavaScriptSerializer().Deserialize<RootObject>(json);

现在您可以使用gridview变量来绑定myCShartData

希望您会有所帮助