使用 C# 将命名空间添加到动态 JSON 对象键
Prepend namespace to dynamic JSON objects keys using C#
我有一个业务案例,我需要获取任何传入的 JSON 有效负载(因此 JSON 对象必须是动态的,而不是由 C# class 预定义)和将给定的名称空间添加到所有包含它的键之前。
例如,如果传入以下有效负载:
{
"property1": "value1",
"property2": 2,
"property3": true,
"property4": {
"myArray": [
{
"arrayProperty1": "im the first object in array",
"arrayProperty2": "some value"
},
{
"arrayProperty1": "im the second object in array",
"arrayProperty2": "some value"
}
]
}
}
然后它需要产生以下输出:
{
"mynamespace.property1": "value1",
"mynamespace.property2": 2,
"mynamespace.property3": true,
"mynamespace.subObj": {
"mynamespace.myArray": [
{
"mynamespace.arrayProperty1": "im the first object in array",
"mynamespace.arrayProperty2": "some value"
},
{
"mynamespace.arrayProperty1": "im the second object in array",
"mynamespace.arrayProperty2": "some value"
}
]
}
}
这可以使用 C# 吗?我尝试在 Whosebug 上搜索任何类似的问题,但这是我得到的最接近的问题(他们使用 javascript):Prepending namespace to all of a JSON object's Keys
您可以使用 Json.Net 的 LINQ-to-JSON API (JTokens) 创建一个简短的辅助方法来完成此操作:
public static string AddPrefixToAllKeys(string json, string prefix)
{
JContainer token = (JContainer)JToken.Parse(json);
// Note: We need to process the descendants in reverse order here
// to ensure we replace child properties before their respective parents
foreach (JProperty prop in token.Descendants().OfType<JProperty>().Reverse().ToList())
{
prop.Replace(new JProperty(prefix + prop.Name, prop.Value));
}
return token.ToString();
}
然后像这样使用它:
string modifiedJson = AddPrefixToAllKeys(originalJson, "mynamespace.");
此处的工作演示:https://dotnetfiddle.net/AdkAO7
我有一个业务案例,我需要获取任何传入的 JSON 有效负载(因此 JSON 对象必须是动态的,而不是由 C# class 预定义)和将给定的名称空间添加到所有包含它的键之前。
例如,如果传入以下有效负载:
{
"property1": "value1",
"property2": 2,
"property3": true,
"property4": {
"myArray": [
{
"arrayProperty1": "im the first object in array",
"arrayProperty2": "some value"
},
{
"arrayProperty1": "im the second object in array",
"arrayProperty2": "some value"
}
]
}
}
然后它需要产生以下输出:
{
"mynamespace.property1": "value1",
"mynamespace.property2": 2,
"mynamespace.property3": true,
"mynamespace.subObj": {
"mynamespace.myArray": [
{
"mynamespace.arrayProperty1": "im the first object in array",
"mynamespace.arrayProperty2": "some value"
},
{
"mynamespace.arrayProperty1": "im the second object in array",
"mynamespace.arrayProperty2": "some value"
}
]
}
}
这可以使用 C# 吗?我尝试在 Whosebug 上搜索任何类似的问题,但这是我得到的最接近的问题(他们使用 javascript):Prepending namespace to all of a JSON object's Keys
您可以使用 Json.Net 的 LINQ-to-JSON API (JTokens) 创建一个简短的辅助方法来完成此操作:
public static string AddPrefixToAllKeys(string json, string prefix)
{
JContainer token = (JContainer)JToken.Parse(json);
// Note: We need to process the descendants in reverse order here
// to ensure we replace child properties before their respective parents
foreach (JProperty prop in token.Descendants().OfType<JProperty>().Reverse().ToList())
{
prop.Replace(new JProperty(prefix + prop.Name, prop.Value));
}
return token.ToString();
}
然后像这样使用它:
string modifiedJson = AddPrefixToAllKeys(originalJson, "mynamespace.");
此处的工作演示:https://dotnetfiddle.net/AdkAO7