7

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? :)

2 Answers 2

10

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

2 Comments

Just in case not every array item is guaranteed to have a wvw_match_id, I'd slightly modify the where condition to .Where(m => m["wvw_match_id"]?.Value<string>() == matchIdToFind) to avoid a runtime exception.
This example should be at www.newtonsoft.com/json/help/html/QueryJsonSelectTokenBasedOnValue.htm
0

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.

7 Comments

isn't it possible without serializing it?
@Jazerix I was just writing an edit about that. Not that I know of, and even if it were I think it's a worse approach.
Well, I would be ever so happy if I could avoid serializing it :3 but if there are no way around it, I guess I'm gonna have to do it :(
@Jazerix I would be shocked if json.NET offered you anyway to do it without serialzing the data. That's not what the library is made for and it would contradict the separation of concerns design principal.
I've been having no problem doing it without so far. Json.net offers its own Linq library. I'm no expert on this area, and I don't mean to offend you in any way, however it makes no sense to me, you can do a JObject.Select, JObject.Where ect... with it, if you have to serialize it first :)
|

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.