在C#中更新嵌套数组中的键值对的值 [英] Update a value of a key value pair in a nested array in C#

查看:39
本文介绍了在C#中更新嵌套数组中的键值对的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下json

  {"audit_entry": {
    "where_uri": "test.com/service/apps/171f0841-825b-4964-8f8c-0869650f14a6",
    "why_uri": "test.com/service/reference/reasons_for_change/43545kjhjhkj0869650f14a6",
    "who_uri": "test.com/service/users/4977dae1-a307-425f-980c-53413fef1b0f",
    "when_audited": "2018-11-13T20:20:39+00:00",
    "what_uri": "test.com/service/subjects/1bc67a71-8549-4ab8-9dd9-e44238198860",
    "what_changed": [
        {
            "attribute_name": "birth_year",
            "attribute_value": "1969",
            "attribute_change": null
        },
        {
            "attribute_name": "subject_reference",
            "attribute_value": "dsdsfg",
            "attribute_change": null
        }
        ]
    }

我希望能够更改"attribute_value"的值,在what_changed的第二个孩子中.即索引[1].我尝试了以下代码:

I want to be able to change the value of "attribute_value" in the second child of what_changed. i.e. index [1]. I have tried the following code:

        JObject jObj = JObject.Parse(jsonText);
        jObj["audit_entry"]["what_changed"]["attribute_name"[1]];

但是我知道我在语法上有问题.任何想法将不胜感激.谢谢

But I know I have an issue with syntax. Any ideas would be much appreciated. Thanks

推荐答案

以下是

Here is the documentation on Getting values by Property Name or Collection Index using JObject.

您可以在代码示例中看到index的用法:

You can see the usage of index in the code example :

string itemTitle = (string)rss["channel"]["item"][0]["title"];

在您的代码示例中,该代码应为:

In you code example it should be :

var toto = (string)jObj["audit_entry"]["what_changed"][1]["attribute_name"];

在线示例

您可以使用修改JSON 作为修改的参考:

You can the use Modifying JSON as reference for the modification:

jObj["audit_entry"]["what_changed"][1]["attribute_name"] = "New Value";

这篇关于在C#中更新嵌套数组中的键值对的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆