将键列表转换为 JSON 格式
Convert list of keys into JSON format
给定结构:
public class ProjectKey
{
public string Key;
public bool Parent;
}
其中 List<ProjectKey>
按字母顺序给出,具有以下值:
Key Parent
--------------------------
text 1
text.ui 1
text.ui.hello 0
text.ui.goodbye 0
text.ui.popup 0
text.ui.project 1
text.ui.project.val 0
text.ui.project.val2 0
如何轻松地将其转换为具有空值的 JSON 格式?例如,上面的列表将给出输出:
{
"text": {
"ui": {
"hello": "",
"goodbye": "",
"popup": "",
"project":{
"val": "",
"val2": ""
}
}
}
}
有什么简单的方法JSON.net可以帮我用这种方式连载吗?还是我需要自己编写转换器?
JObject
in JSON.NET 似乎特别适合这样的事情,如果你不想创建一个自定义类型来表示你的层次结构:
public string SerializeProjectKeys(List<ProjectKey> pks)
{
JObject toSerialize = new JObject();
foreach (ProjectKey pk in pks)
{
string[] keyParts = pk.Key.Split('.');
JObject currentObj = toSerialize;
for (int i = 0; i < keyParts.Length; i++)
{
string keyPart = keyParts[i];
if (i == keyParts.Length - 1)
{
if (pk.Parent)
{
currentObj.Add(keyPart, new JObject());
}
else
{
currentObj.Add(keyPart, string.Empty);
}
}
else
{
currentObj = (JObject)currentObj[keyPart];
}
}
}
return toSerialize.ToString();
}
我知道 JSON 对字符串的操作不太好。但我从未使用 C# 创建 JSON 个对象。可能您的问题不包含所有数据,但我认为这可能对主要思想有所帮助:
List<ProjectKey> projectKeys = new List<ProjectKey>();
projectKeys.Add(new ProjectKey("text", true));
projectKeys.Add(new ProjectKey("text.ui", true));
projectKeys.Add(new ProjectKey("text.ui.hello", false));
projectKeys.Add(new ProjectKey("text.ui.goodbye", false));
projectKeys.Add(new ProjectKey("text.ui.popup", false));
projectKeys.Add(new ProjectKey("text.ui.project", true));
projectKeys.Add(new ProjectKey("text.ui.project.val", false));
projectKeys.Add(new ProjectKey("text.ui.project.val2", false));
StringBuilder output = new StringBuilder();
for (int i = 0; i < projectKeys.Count; i++)
{
if (projectKeys[i].Parent)
{
// If current key is parent - do not close bracket.
output.AppendLine(string.Format("\"{0}\": {{", projectKeys[i].Key.Split('.').Last()));
}
else
{
// If this is no ones parent - close bracket.
output.AppendLine(string.Format("\"{0}\": \"\"", projectKeys[i].Key.Split('.').Last()));
}
}
// Append as many closing brackets as there are many parents.
for (int i = 0; i < projectKeys.Where(x => x.Parent).Count(); i++)
{
output.AppendLine("}");
}
Console.Write(output.ToString());
恕我直言,这是一种包含信息的奇怪树 "am I parent" - 我建议包含 "my parent" 属性.
给定结构:
public class ProjectKey
{
public string Key;
public bool Parent;
}
其中 List<ProjectKey>
按字母顺序给出,具有以下值:
Key Parent
--------------------------
text 1
text.ui 1
text.ui.hello 0
text.ui.goodbye 0
text.ui.popup 0
text.ui.project 1
text.ui.project.val 0
text.ui.project.val2 0
如何轻松地将其转换为具有空值的 JSON 格式?例如,上面的列表将给出输出:
{
"text": {
"ui": {
"hello": "",
"goodbye": "",
"popup": "",
"project":{
"val": "",
"val2": ""
}
}
}
}
有什么简单的方法JSON.net可以帮我用这种方式连载吗?还是我需要自己编写转换器?
JObject
in JSON.NET 似乎特别适合这样的事情,如果你不想创建一个自定义类型来表示你的层次结构:
public string SerializeProjectKeys(List<ProjectKey> pks)
{
JObject toSerialize = new JObject();
foreach (ProjectKey pk in pks)
{
string[] keyParts = pk.Key.Split('.');
JObject currentObj = toSerialize;
for (int i = 0; i < keyParts.Length; i++)
{
string keyPart = keyParts[i];
if (i == keyParts.Length - 1)
{
if (pk.Parent)
{
currentObj.Add(keyPart, new JObject());
}
else
{
currentObj.Add(keyPart, string.Empty);
}
}
else
{
currentObj = (JObject)currentObj[keyPart];
}
}
}
return toSerialize.ToString();
}
我知道 JSON 对字符串的操作不太好。但我从未使用 C# 创建 JSON 个对象。可能您的问题不包含所有数据,但我认为这可能对主要思想有所帮助:
List<ProjectKey> projectKeys = new List<ProjectKey>();
projectKeys.Add(new ProjectKey("text", true));
projectKeys.Add(new ProjectKey("text.ui", true));
projectKeys.Add(new ProjectKey("text.ui.hello", false));
projectKeys.Add(new ProjectKey("text.ui.goodbye", false));
projectKeys.Add(new ProjectKey("text.ui.popup", false));
projectKeys.Add(new ProjectKey("text.ui.project", true));
projectKeys.Add(new ProjectKey("text.ui.project.val", false));
projectKeys.Add(new ProjectKey("text.ui.project.val2", false));
StringBuilder output = new StringBuilder();
for (int i = 0; i < projectKeys.Count; i++)
{
if (projectKeys[i].Parent)
{
// If current key is parent - do not close bracket.
output.AppendLine(string.Format("\"{0}\": {{", projectKeys[i].Key.Split('.').Last()));
}
else
{
// If this is no ones parent - close bracket.
output.AppendLine(string.Format("\"{0}\": \"\"", projectKeys[i].Key.Split('.').Last()));
}
}
// Append as many closing brackets as there are many parents.
for (int i = 0; i < projectKeys.Where(x => x.Parent).Count(); i++)
{
output.AppendLine("}");
}
Console.Write(output.ToString());
恕我直言,这是一种包含信息的奇怪树 "am I parent" - 我建议包含 "my parent" 属性.