简体   繁体   中英

How can I get parameters out of a string and store them in variables? (C#)

So I have a single string downloaded that will have this format:

  "MatchId": "83570b03-2963-4b44-834c-bf77c74f2d0a",
  "MatchType": 3,
  "GameMode": 3,
  "SeasonId": null,
  "PlaylistId": "282fc197-7bf1-4865-81ec-a312d07567b6",
  "Teams": {
    "1": {
      "TeamSize": 2
    },
    "2": {
      "TeamSize": 2
    }
  },
  "MapId": "skirmish\\design\\MP_Veteran\\MP_Veteran",
  "MatchStartDate": {
    "ISO8601Date": "2017-02-24T21:20:42.024Z"
  },
  "PlayerMatchDuration": "PT35M39.4550853S",
  "PlayerIndex": 3,
  "TeamId": 1,
  "TeamPlayerIndex": 1,
  "LeaderId": 3,
  "PlayerCompletedMatch": true,
  "PlayerMatchOutcome": 2,
  "XPProgress": {
    "PreviousTotalXP": 23300,
    "GameplayXP": 600,
    "ChallengesXP": 0,
    "UpdatedTotalXP": 23900,
    "CompletedSpartanRanks": []

What I want to know is if I have a separate class with variables such as 'MatchId', 'MatchType', 'GameMode' etc. How can I input this string and automatically store all of the values into the correct variable?
Thanks :)

This looks like JSON, does this have a surrounding { and } ? If so you need to parse it as JSON which should be pretty easy if you use something like what Icepickle suggested (NewtonSoft.JSON)

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