使用路径和值更新JSON对象

时间:2016-03-17 09:48:37

标签: c# json json.net

我有一个csv文件,其中包含以下格式的路径和值:

path;value
prop1.prop2.1;hello
prop1.prop2.2;world
prop1.prop2.3;!
prop1.prop3.test;hi
prop1.prop4;value

我想把它当成json:

{
  "prop1": {
    "prop2": {
      "1": "hello",
      "2": "world",
      "3": "!"
    }
    "prop3": {
      "test": "hi"
    }
    "prop4": "value"
  }    
}

我已经像这样解析了csv文件:

Dictionary<string, string> dict = new Dictionary<string, string>();
while (csv.Read())
{
  string path = csv.GetField<string>(0);
  string value = csv.GetField<string>(1);
  dict.Add(path, value);
}

你能帮我解决一下方法,它将使用JSON.Net库从这本词典中创建JSON。 当然原始文件中的属性可能不同。

3 个答案:

答案 0 :(得分:1)

您可以使用此函数将Json作为字典中的字符串返回

public static string BuildJson(Dictionary<string, string> dict)
{
    dynamic expando = new ExpandoObject();

    foreach (KeyValuePair<string, string> pair in dict.Where(x => !string.Equals(x.Key, "path")))
    {
        string[] pathArray = pair.Key.Split('.');
        var currentExpando = expando as IDictionary<string, Object>;

        for (int i = 0; i < pathArray.Count(); i++)
        {
            if (i == pathArray.Count() - 1)
            {
                currentExpando.Add(pathArray[i], pair.Value);
            }
            else
            {
                if (!currentExpando.Keys.Contains(pathArray[i]))
                {
                    currentExpando.Add(pathArray[i], new ExpandoObject());
                }
                currentExpando = currentExpando[pathArray[i]] as IDictionary<string, Object>;
            }
        }
    }

    JObject o = JObject.FromObject(expando);
    return o.ToString();
}

您需要添加使用System.Dynamic;

答案 1 :(得分:0)

您可以利用System.Dynamic.ExpandoObject

public string ToJson(Dictionary<string, string> props)
{
    IDictionary<string, object> json = new System.Dynamic.ExpandoObject() as IDictionary<string, object>;

    foreach (var prop in props)
    {
        string path = prop.Key;
        if (String.IsNullOrWhiteSpace(path)) continue;
        string[] keys = path.Split('.');

        string value = prop.Value;

        var cursor = json;

        for (int i = 0; i < keys.Length; i++)
        {
            object innerJson;
            if (!cursor.TryGetValue(keys[i], out innerJson))
                cursor.Add(keys[i], new System.Dynamic.ExpandoObject() as IDictionary<string, object>);
            if (i == keys.Length - 1)
                cursor[keys[i]] = value;

            cursor = cursor[keys[i]] as IDictionary<string, object>;
        }

    }

    return JsonConvert.SerializeObject(json);
}

答案 2 :(得分:0)

不使用动态:

DELIMITER $$
CREATE PROCEDURE PROC()
BEGIN
    ALTER ...... ;
    DELETE ..... ;
END $$
DELIMITER ;

添加以下方法:

Dictionary<string, object> dictionary = new Dictionary<string, object>();
while (csv.Read())
{
  string path = csv.GetField<string>(0);
  string value = csv.GetField<string>(1);
  dictionary = GetHierarchy(path, value);
}
string serialized = JsonConvert.SerializeObject(dictionary);
Console.WriteLine(serialized);

生成预期输出