【问题标题】:hello Deserialize nested JSON into C# objects你好 将嵌套的 JSON 反序列化为 C# 对象
【发布时间】:2018-08-24 01:23:12
【问题描述】:
"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.制作类来表示您的数据(或在 Visual Studio 中使用“特殊粘贴”)。 2. 将 JSON 反序列化为您的类。

标签: c# json gridview


【解决方案1】:

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

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

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; }
}

现在你需要Deserializejson 来上课。

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

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

希望对你有所帮助

【讨论】:

    猜你喜欢
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 2017-12-28
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多