且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除动态JSON字符串名称中的空格

更新时间:2022-03-31 05:45:08

I猜测没有其他方法可以修改JsonObject上的空格而不是迭代每个节点。你可能会做类似下面的事情。虽然您的JSON字符串与示例不同,但这个想法几乎相同。

I guess there's no other way to modify the white spaces on the JsonObject than to iterate on each node. You can probably do something like below. Although your JSON string is different from the example, the idea is pretty much the same.
string jsonString = "{nodes:[{'Node 1':'value1','Node 2':'value2'}]}";

JObject jsonObject = JObject.Parse(jsonString);
JObject newJsonObject = new JObject();
JProperty property;
foreach(var token in jsonObject["nodes"].Values()){
    property = (JProperty)token;
    newJsonObject.Add(property.Name.Replace(" ", "_"), property.Value);
}
var xml = (XmlDocument)JsonConvert.DeserializeXmlNode(newJsonObject.ToString(),"root");