删除动态JSON字符串名称中的空格 [英] Remove white spaces in Names of dynamic JSON string

查看:214
本文介绍了删除动态JSON字符串名称中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我将JSON字符串转换为XML时遇到问题,因为名称中有空格。我正在动态获取JSON。所以无法更改元素名称。有没有办法从元素名称中删除空格或用'_'替换空格



请提供解决方案。下面提供的示例JSON字符串:

Hi,

I am facing a problem when converting a JSON string to XML as there are white spaces in the names.I am getting the JSON dynamically..so cannot change the element names.Is there a way to remove the whitespaces from the element names or replace the white space with an '_'

Please provide a solution. Sample JSON string provided below :

[{"Team":"Customer Target & Solution",
	"SubTeam":"MDS",
	"Location":"Hyderabad",
"NBKID":"NBKQY0T",
"AssociateName":"Test1",
"Communication":null,
"Development":"85",
"KT Effort":"7",
"Production Support":"64",
"Project Related Training":"4",
"Release Support":null,
"Admin Support":null,
"Community Service":null,
"Leave":null,
"Non-Project Related KT":null,
"Non-Project Related Training":null,
"Other Activity":null,
"Project Utilization":"100.00",
"Gross Utilization":"100.00",
"id":null},
{"Team":"Customer Target & Solution",
"SubTeam":"TLS",
"Location":"Guragon",
"NBKID":"NBKUVKV",
"AssociateName":"Test2",
"Communication":"11",
"Development":"68",
"KT Effort":null,
"Production Support":"64",
"Project Related Training":null,
"Release Support":null,
"Admin Support":null,
"Community Service":null,
"Leave":"16",
"Non-Project Related KT":null,
"Non-Project Related Training":null,
"Other Activity":null,
"Project Utilization":"99.31",
"Gross Utilization":"99.31",
"id":null}]

推荐答案

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");


这篇关于删除动态JSON字符串名称中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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