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

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

My current code base asks for the list to be in JTokens do you know how I might get the list that way? JToken that represents the zombies object.
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. stackoverflow.com/questions/38211719/…
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
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)

Comments

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.

1 Comment

nice use of JsonProperty

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.