Patrick
Patrick

Reputation: 7722

Json generated Object Graph query using Linq?

I'm working with a json file containing the following:

{
"objects":[ 
        {"object": 1, 
            "data":{
                "name": "object 1",
                "priority_threshold": "6000", 
                "email_threshold": "2000"
        }},
        {"object": 3,
            "data":{
                "name": "object 3",
                "priority_threshold": "5000",
                "email_threshold": "2000"
        }},
        {"object": 5,
            "data":{
                "name": "object 5",
                "priority_threshold": "5000",
                "email_threshold": "1000"
        }},
        {"object": 6,
            "data": {
                "name": "object 6",
                "priority_threshold": "4000",
                "email_threshold": "2000"
        }
    }
]}

the .json file is an embedded file and is being returned as a string.

Then from the string I am deserializing the object using System.Web.Script.Serialization.JavaScriptSerializer to do the following:

Dictionary<string, object> toConfigGraph = (Dictionary<string, object> toSerializer.DeserializeObject(psJsonString);
object[] toEventServiceConfig = (object[])toConfigGraph["objects"];

The problem running into is that I only want to return the data for a particular object using the object ID, but I'm unsure as to the best process. The I would like to implement a Linq solution, but as of now I'm not even sure if that will work since toConfigGraph["applications"] returns an array of objects based on the structure of the json.

Any suggestions would be greatly appreciated.

I'd rather NOT have to iterate through the object array.

Thanks.

Upvotes: 1

Views: 262

Answers (1)

Mutt
Mutt

Reputation: 935

        Dictionary<string, object> toObj = (Dictionary<string, object>)toEventServiceConfig.Where(o => Int32.Parse(((Dictionary<string, object>)o)["object"].ToString()) == 1).First<object>();
        Dictionary<string, object> toData = (Dictionary<string, object>)toObj["data"];

Upvotes: 2

Related Questions