Jazerix
Jazerix

Reputation: 4801

Json.Net Select Object based on a value

I have a Json object that looks like this:

{
     wvw_matches: [
          {
               wvw_match_id: "1-4",
               red_world_id: 1011,
               blue_world_id: 1003,
               green_world_id: 1002,
               start_time: "2013-09-14T01:00:00Z",
               end_time: "2013-09-21T01:00:00Z"
          },
          {
               wvw_match_id: "1-2",
               red_world_id: 1017,
               blue_world_id: 1021,
               green_world_id: 1009,
               start_time: "2013-09-14T01:00:00Z",
               end_time: "2013-09-21T01:00:00Z"
          }
     ]
}

It contains a lot more objects in the array than the example above shows. Anyway, I need to select the Json object based on the wvw_match_id.

How would I achieve this? :)

Upvotes: 7

Views: 18253

Answers (2)

evanmcdonnal
evanmcdonnal

Reputation: 48114

You should just serialize it as you normally would then filter the array with LINQ. So I'm just going to assume you've defined a class for the objects in this array, call it MyObj

MyObj[] myObjects = serializer.Deserialize<MyObj[]>(jsonAsAString);
var filteredObjs = myObjects.Where(x => x.wvw_match_id == "the id i'm filtering on");

Keep in mind it's much easier to work with native C# objects than JSON. JSON.NET is for doing the serialization/deserialization. Filtering your collection is something you want to do with iteration or LINQ.

Upvotes: 0

Brian Rogers
Brian Rogers

Reputation: 129787

Since it seems from the comments that you're already semi-comfortable with the idea of using JObject and Linq, here is an example program demonstrating how to get a specific match from your JSON by ID using that approach:

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {
            wvw_matches: [
                {
                    wvw_match_id: ""1-4"",
                    red_world_id: 1011,
                    blue_world_id: 1003,
                    green_world_id: 1002,
                    start_time: ""2013-09-14T01:00:00Z"",
                    end_time: ""2013-09-21T01:00:00Z""
                },
                {
                    wvw_match_id: ""1-2"",
                    red_world_id: 1017,
                    blue_world_id: 1021,
                    green_world_id: 1009,
                    start_time: ""2013-09-14T01:00:00Z"",
                    end_time: ""2013-09-21T01:00:00Z""
                }
            ]
        }";

        string matchIdToFind = "1-2";
        JObject jo = JObject.Parse(json);

        JObject match = jo["wvw_matches"].Values<JObject>()
            .Where(m => m["wvw_match_id"].Value<string>() == matchIdToFind)
            .FirstOrDefault();

        if (match != null)
        {
            foreach (JProperty prop in match.Properties())
            {
                Console.WriteLine(prop.Name + ": " + prop.Value);
            }
        }
        else
        {
            Console.WriteLine("match not found");
        }

    }
}

Output:

wvw_match_id: 1-2
red_world_id: 1017
blue_world_id: 1021
green_world_id: 1009
start_time: 9/14/2013 1:00:00 AM
end_time: 9/21/2013 1:00:00 AM

Upvotes: 10

Related Questions