开发者

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

Example:

I have the following JSON object.

{"currentVersion" : "10.0", 
  "folders" : [], 
  "services" : [
    {"name" : 开发者_如何学C"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" : "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.

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.

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?


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.

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.

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜