使用 Linq Json 生成对象图查询?

发布于 2024-10-19 22:15:22 字数 1326 浏览 2 评论 0原文

我正在使用包含以下内容的 json 文件:

{
"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"
        }
    }
]}

.json 文件是嵌入式文件,并且作为字符串返回。

然后,我使用 System.Web.Script.Serialization.JavaScriptSerializer 从字符串反序列化该对象,以执行以下操作:

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

遇到的问题是我只想使用对象 ID 返回特定对象的数据,但我不确定最佳流程。我想实现一个 Linq 解决方案,但到目前为止我什至不确定这是否可行,因为 toConfigGraph["applications"] 返回基于 json 结构的对象数组。

任何建议将不胜感激。

我宁愿不必遍历对象数组。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄风 2024-10-26 22:15:22
        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"];
        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"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文