visc
visc

Reputation: 4959

Deserialize selected JSON object from JSON string

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

Upvotes: 1

Views: 1278

Answers (3)

Skintkingle
Skintkingle

Reputation: 1579

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.

Upvotes: 1

Giltanas
Giltanas

Reputation: 32

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)

Upvotes: 1

Sam Marion
Sam Marion

Reputation: 690

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

Upvotes: 1

Related Questions