-2

I have the following JSON file and was hoping someone could tell me how to simply access the nested "player_status" and "previous_teams" values using the JsonConvert.DeserializeObject() Method. Any references tot tutorials better than the outdated sites I have seen would be helpful too. enter image description here

Thank you

6
  • See stackoverflow.com/questions/35206019/…. You'll need to create a class structure starting from the parent object. In order to retrieve just sub-properties without parsing the parent object, you'd need to write a complicated parser to pull out each property by name, which would likely be slower and more difficult than just creating classes for the entire object structure. Commented Apr 13, 2021 at 3:48
  • @Joseph, thank you for the response. I did look into your initial referenced link and made something like that, but wanted to more specifically look into how parsing the parent object would work. Would you know how to do that in this instance? Commented Apr 13, 2021 at 4:08
  • 2
    Please include your JSON as formatted text in your question, not as an image. Commented Apr 13, 2021 at 4:39
  • Will do. Thank you Llama Commented Apr 13, 2021 at 4:59
  • newtonsoft.com/json/help/html/SerializingJSONFragments.htm Commented Apr 14, 2021 at 7:37

2 Answers 2

1

Option 1 is to parse or query json. please see the official Querying JSON with LINQ or Querying JSON with SelectToken for Json.NET or JsonDocument.Parse for the new System.Text.Json serialiser.

If you want/need to use JsonConvert.DeserializeObject then you will need to create a set of classes that represents your data.

public class League 
{
   public List<Team> Details { get; set;} 
}
public class Team 
{
 public List<AboutPlayers> Players {get; set;}
}

public class AboutPlayers 
{
 public List<Capatin> Captains {get; set;}
}

public class Captain 
{
 public string Player_Status{get; set;}
 public PlayerHistory Player_History {get; set;}
}

(...)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the response. I prefer your first option and didn't even know that was an option :)
1

You can use JSON Path to query it. See: https://www.newtonsoft.com/json/help/html/QueryJsonSelectToken.htm

    JObject json = JObject.Parse("{ json string }");
    var playerStatus = json.SelectToken("details[0].players.captains[0].player_status");
    var previousTeams = json.SelectToken("details[0].players.captains[0].player_history.previous_teams");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.