如何最好地利用Json。NET修改现有JSON对象的部分?

时间:2021-07-08 20:08:38

Example:

例子:

I have the following JSON object.

我有以下JSON对象。

{"currentVersion" : "10.0", 
  "folders" : [], 
  "services" : [
    {"name" : "nyc", "type" : "MapServer"}, 
    {"name" : "philly", "type" : "MapServer"}
  ]
}

I want to be able to remove one or more items from the services array based on the value of the name attribute. For instance, I want to search for name="nyc" and remove the following object from the array.

我希望能够根据name属性的值从服务数组中删除一个或多个项。例如,我要搜索name="nyc"并从数组中删除以下对象。

{"name" : "nyc", "type" : "MapServer"}

The end result is an object that looks like this:

最终的结果是一个像这样的对象:

{"currentVersion" : "10.0", 
  "folders" : [], 
  "services" : [
    {"name" : "philly", "type" : "MapServer"}
  ]
}

I have been able to create new JSON object fairly easily and I can read existing ones. However, I am having difficultly determining the best way to modify an existing JSON object. Particularly as it relates to querying for specific objects within the JSON object.

我可以很容易地创建新的JSON对象,并且可以读取现有的对象。但是,我很难确定修改现有JSON对象的最佳方式。特别是当它涉及到查询JSON对象中的特定对象时。

One method that I have uncovered (specific to this example) is to rebuild portions of the JSON that I want modified and replace the tokens as necessary. However, I have to think there is a better way. This code is shown below.

我发现的一种方法(特定于本例)是重新构建希望修改的JSON部分,并在必要时替换令牌。然而,我不得不认为还有更好的办法。这段代码如下所示。

string json = @"{""currentVersion"" : ""10.0"", 
                    ""folders"" : [], 
                    ""services"" : [
                        {""name"" : ""nyc"", ""type"" : ""MapServer""}, 
                        {""name"" : ""philly"", ""type"" : ""MapServer""}
                    ]
                }";

string[] keepList = new string[] { "nyc" };

JObject o = JObject.Parse(json);
JArray services = (JArray)o["services"];
JArray newServices = new JArray();

foreach (JToken service in services)
{
    foreach (string keeper in keepList)
    {
        if ((string)service["name"] == keeper)
        {
            newServices.Add(service);
            break;
        }
    }
}

services.Replace(newServices);

string output = o.ToString();

How can I best utilize Json.NET to modify parts of an existing JSON object?

如何最好地利用Json。NET修改现有JSON对象的部分?

1 个解决方案

#1


8  

I have yet to determine an alternate way to modify an existing JSON than what was provided in the original question. If a better way surfaces I'll gladly accept that as the correct answer.

我还没有确定修改现有JSON的替代方法,而不是原始问题中提供的方法。如果有更好的方法出现,我将欣然接受这一正确答案。

In the meantime, the solution that I have implemented is to replace the parts of the JSON object I need modified with newly created objects. An example is shown below.

同时,我实现的解决方案是用新创建的对象替换需要修改的JSON对象的部分。下面是一个示例。

string json = @"{""currentVersion"" : ""10.0"", 
                    ""folders"" : [], 
                    ""services"" : [
                        {""name"" : ""nyc"", ""type"" : ""MapServer""}, 
                        {""name"" : ""philly"", ""type"" : ""MapServer""}
                    ]
                }";

string[] keepList = new string[] { "nyc" };

JObject o = JObject.Parse(json);
JArray services = (JArray)o["services"];
JArray newServices = new JArray();

foreach (JToken service in services)
{
    foreach (string keeper in keepList)
    {
        if ((string)service["name"] == keeper)
        {
            newServices.Add(service);
            break;
        }
    }
}

services.Replace(newServices);

string output = o.ToString();

#1


8  

I have yet to determine an alternate way to modify an existing JSON than what was provided in the original question. If a better way surfaces I'll gladly accept that as the correct answer.

我还没有确定修改现有JSON的替代方法,而不是原始问题中提供的方法。如果有更好的方法出现,我将欣然接受这一正确答案。

In the meantime, the solution that I have implemented is to replace the parts of the JSON object I need modified with newly created objects. An example is shown below.

同时,我实现的解决方案是用新创建的对象替换需要修改的JSON对象的部分。下面是一个示例。

string json = @"{""currentVersion"" : ""10.0"", 
                    ""folders"" : [], 
                    ""services"" : [
                        {""name"" : ""nyc"", ""type"" : ""MapServer""}, 
                        {""name"" : ""philly"", ""type"" : ""MapServer""}
                    ]
                }";

string[] keepList = new string[] { "nyc" };

JObject o = JObject.Parse(json);
JArray services = (JArray)o["services"];
JArray newServices = new JArray();

foreach (JToken service in services)
{
    foreach (string keeper in keepList)
    {
        if ((string)service["name"] == keeper)
        {
            newServices.Add(service);
            break;
        }
    }
}

services.Replace(newServices);

string output = o.ToString();