I have a large object coming back and I only need a fraction of the data. I was looking at the example here. I essentially want to do the same thing except the problem is I would have an array of "error" objects.
So, it would look like this
{
"short": {
"original": "http://www.foo.com/",
"short": "krehqk",
"error": [
{
"code": 0,
"msg": "No action taken"
},
{
"code": 0,
"msg": "No action taken"
}
]
}
}
Is there an easy way to accomplish this using JObject.Parse or maybe even Linq to JSON? Am I better off just using JsonConvert.DeserializeObject and just not including the properties/objects I don't need in the .NET objects I have created?
UPDATE Using the JSON above here is my test...
[TestMethod]
public void ParseStuffTest()
{
JObject json = JObject.Parse(shortJson);
Shortie shortie = new Shortie
{
Original = (string)json["short"]["original"],
Short = (string)json["short"]["short"],
Error = new ShortError
{
Code = (int)json["short"]["error"]["code"],
ErrorMessage = (string)json["short"]["error"]["msg"]
}
};
Assert.IsNotNull(shortie);
}
public class Shortie
{
[JsonProperty]
public string Original { get; set; }
[JsonProperty]
public string Short { get; set; }
[JsonProperty]
public List<ShortError> Error{ get; set; }
}
public class ShortError
{
[JsonProperty]
public int Code { get; set; }
[JsonProperty]
public string ErrorMessage { get; set; }
}
Here is the error: Cannot implicitly convert type 'Unit_Tests.Test.ShortError' to 'System.Collections.Generic.List'
What am I missing?