Home > Mobile >  Parse data from multiple json arrays C#
Parse data from multiple json arrays C#

Time:06-02

I have this JSON, and I'm trying to get each name, and place id, creator name, etcetera from the JSON data, I haven't figured out how to, I am using Newtonsoft.Json so then I will be able to write them in the console together.

Here's the JSON

{
  "games": [
    {
      "creatorId": 209596022,
      "creatorType": "User",
      "totalUpVotes": 12,
      "totalDownVotes": 1,
      "universeId": 2044766328,
      "name": "Char les Calvin Memorial",
      "placeId": 5764830729,
      "playerCount": 0,
      "price": null,
      "analyticsIdentifier": null,
      "gameDescription": "test",
      "genre": ""
    },
    {
      "creatorId": 209596022,
      "creatorType": "User",
      "totalUpVotes": 13,
      "totalDownVotes": 2,
      "universeId": 2043766328,
      "name": "Char les C3lvin Memorial",
      "placeId": 5763830729,
      "playerCount": 0,
      "price": null,
      "analyticsIdentifier": null,
      "gameDescription": "tedst",
      "genre": ""
    }
  ],
  "suggestedKeyword": null,
  "correctedKeyword": null,
  "filteredKeyword": null,
  "hasMoreRows": true,
  "nextPageExclusiveStartId": null,
  "featuredSearchUniverseId": null,
  "emphasis": false,
  "cutOffIndex": null,
  "algorithm": "GameSearchUsingSimilarQueryService",
  "algorithmQueryType": "Bucketboost",
  "suggestionAlgorithm": "GameSuggestions_V2",
  "relatedGames": [],
  "esDebugInfo": null
}

CodePudding user response:

there are a lot of ways to get data from json. It depends what do you need. For example you can learn this

    List<Game> games = JObject.Parse(json)["games"].ToObject<List<Game>>();

how to use

    Game game = games.Where(g => g.name == "Char les C3lvin Memorial").FirstOrDefault();

Game class

public class Game
{
    public int creatorId { get; set; }
    public string creatorType { get; set; }
    public int totalUpVotes { get; set; }
    public int totalDownVotes { get; set; }
    public int universeId { get; set; }
    public string name { get; set; }
    public object placeId { get; set; }
    public int playerCount { get; set; }
    public object price { get; set; }
    public object analyticsIdentifier { get; set; }
    public string gameDescription { get; set; }
    public string genre { get; set; }
}
  • Related