1

I have a JSON string that looks similar to this:

{
    "automatic" : "true",
    "brainstorm" : "1000",
    "zombies" : [{ "Name" : "Fred", "Grrh" : "50" }, { "Name" : "Sally", "Grrh" : "67" }, { "Name" : "Chris", "Grrh" : "23" }],
    "nightSkyRadius" : "30"
    ... could be anything here or at the same level as zombies ...
}

So, in my scenario I know the Zombie objects in the array will always be the same. But I don't know anything else beyond that. That is to say, there could be any number of values at the same root as the zombies value.

So my question is, how do I using Json.NET deserialize only my zombies? I don't know what the other values are (if values is the right term) so I cannot just create an object that describes the incoming Json string. So I'm thinking I can just pick zombies out of the json string and deserialize it then.

But, I thought, I'd have to write a string parser that would pull zombies out.. that seems like an extra unnecessary step. Can't Json.NET do this for me?

Also, I've tried JsonConvert.DeserializeObject<dynamic>(responseString); but that can only handle the case when one zombie is specified in the response string.

Thanks, I hope zombies made this problem seem cooler lol

visc
  • 4,794
  • 6
  • 32
  • 58

3 Answers3

1

Create a Zombie class and parse the json into that. Newtonsoft is smart enough to decode it for you

public class zombies
{
    public string Name;
    public int Grrh;
}

Now you can do

var zombies = JsonConvert.DeserializeObject<List<zombies>>(responseString);
Sam Marion
  • 690
  • 1
  • 4
  • 17
  • My current code base asks for the list to be in `JToken`s do you know how I might get the list that way? `JToken` that represents the `zombies` object. – visc Aug 30 '17 at 15:37
  • 1
    Personally I don't think you are doing yourself a favor using JTokens for this as its really only used when you aren't sure whats going to be inside it. Since you only want the zombie object deserializing it into a defined object is going to make working with it much easier. https://stackoverflow.com/questions/38211719/json-net-why-use-jtoken-ever – Sam Marion Aug 30 '17 at 15:41
  • Right, but in my situation I can't exactly change the implementation. Good point though. EDIT: I'm working with a sql interface that takes `JToken` – visc Aug 30 '17 at 19:38
1

If you need only array of zombies you can simple deserialize object with only one auto property array of zombies

public class Zombie {
public string Name {get;set;}
public string Grrh {get;set;}
}

public class Zombies {
public IEnumerable<Zombie> ZombieCollection {get;set;}
}

Then

JsonConvert.DeserializeObject<Zombies>(responseString)
Giltanas
  • 32
  • 5
1

you can pass that entire Json Object in to an object with just a list of Zombies as a collection on it. Specifying the JsonPropertyAttribute it will then deserialize the zombies property only, and ignore everything else it cant map to a thing on your object.

assuming a zombie class of:

public class Zombie
{
    public string Name { get; set; }
    public string Grrh {get; set; }
}

and a class to hold the entire json object (which will just be the zombie collection)

public class MyZombieJsonData
{
    [JsonProperty(PropertyName = "zombies")]
    public List<Zombie> ZombieList { get; set; }
}

then wherever you have access to the json string, you can do:

JsonConvert.DeserializeObject<MyZombieJsonData>(theJsonDataAsAString);

where theJsonDataAsAString is your entire Json data, including the stuff you dont know about and dont want to deserialize, as a string type.

Skintkingle
  • 1,579
  • 3
  • 16
  • 28