将文本转换为CSV以进行json反序列化

时间:2015-09-26 12:03:45

标签: c# json csv

我有一个txt文件的以下内容,实际上这里是原始链接: http://cdn.dota2.com/apps/570/scripts/items/items_game.9e449667cd3ba55c9c545c2a4a1825da541057b0.txt

"items"
{
    "default"
    {
        "name"      "default"
        "hidden"        "1"
        "item_class"        "dota_item_wearable"
        "item_name"     "#TF_Default_ItemDef"
        "item_slot"     "weapon"
        "item_quality"      "normal"
        "min_ilevel"        "1"
        "max_ilevel"        "1"
    }
    "20887"
    {
        "name"      "Level 1000 Compendium"
        "prefab"        "misc"
        "creation_date"     "2015-08-14"
        "expiration_date"       "2016-05-01 00:00:00"
        "image_inventory"       "econ/tools/aegisholder"
        "item_description"      "#DOTA_Item_Desc_Level_1000_Compendium"
        "item_name"     "#DOTA_Item_Level_1000_Compendium"
        "item_rarity"       "immortal"
        "item_type_name"        "#DOTA_WearableType_Relic_of_Prestige"
        "static_attributes"
        {
            "cannot trade"
            {
                "attribute_class"       "cannot_trade"
                "value"     "1"
            }
            "cannot delete"
            {
                "attribute_class"       "cannot_delete"
                "value"     "1"
            }
        }
        "used_by_heroes"        "0"
    }
    "15323"
    {
        "name"      "Gem of Taegeuk"
        "prefab"        "socket_gem"
        "creation_date"     "2013-10-16"
        "disable_style_selector"        "1"
        "image_inventory"       "econ/tools/samtaegeuk"
        "item_description"      "#DOTA_Item_Desc_Gem_of_Taegeuk"
        "item_name"     "#DOTA_Item_Gem_of_Taegeuk"
        "item_rarity"       "rare"
        "item_type_name"        "#DOTA_WearableType_Relic_of_Prestige"
        "static_attributes"
        {
            "gem type"
            {
                "attribute_class"       "gem type"
                "value"     "3"
            }
            "cannot trade"
            {
                "attribute_class"       "cannot_trade"
                "value"     "1"
            }
            "gem quality"
            {
                "attribute_class"       "gem quality"
                "value"     "14"
            }
        }
        "used_by_heroes"        "0"
    }
}

我正在尝试将其转换为CSV文件,以便我可以轻松地将CSV转换为JSON并进行反序列化,并使用此文本文件(数据值格式)中的属性和值填充我的类。

我有以下代码解析txt文件:

Treenode rarities = root.Child["items_game"];
                    StreamWriter sw = new StreamWriter("items_game.csv");
                    sw.AutoFlush = true;
                    RecursiveReadSchema(rarities, sw, 0);

private static void RecursiveReadSchema(Treenode rarities, StreamWriter sw, int depth)
        {
            foreach(KeyValuePair<string, Treenode> n in rarities.Child)
            {
                //csv = "," + n.Key;
                string csv = new String(',', depth) + n.Key + "\n";
                if(n.Value.Data.Count > 0)
                {
                    foreach(KeyValuePair<string, string> keyValuePair in n.Value.Data)
                    {
                        csv += Environment.NewLine + "," + keyValuePair.Key + "," + keyValuePair.Value;
                    }
                }

                sw.WriteLine(csv);
                RecursiveReadSchema(n.Value, sw, depth + 1);
            }
        }

我的问题是“static_attributes”的一部分,它们没有放在CSV文件中的适当位置以及它的元素。现在,如果有人知道将这个txt文件直接转换为JSON的更好方法,我会去寻找解决方案,但我没有在网上找到任何东西。现在,我正在尝试将此txt解析为treenode,然后将treenode解析为CSV文件,从那里到JSON,从JSON到我的班级。

我使用的KVParser课程来自:http://pastebin.com/C9t3y3H7

谢谢,

编辑:

这一行:

SchemaResult schemaResult = JsonConvert.DeserializeObject<SchemaResult>(result);

这是我的SchemaResult类:

protected class SchemaResult
{
    public Schema Items_game 
    {
        get;
        set;
    }
}

public class Schema
    {
        [JsonProperty("items")]
        public Dictionary<string, Item> Items
        {
            get;
            set;
        } 

        public class Prefabs
        {
            [JsonProperty("item_class")]
            public int Item_Class
            {
                get;
                set;
            }

            [JsonProperty("item_type_name")]
            public string Item_Type_Name
            {
                get;
                set;
            }
        }

        public class Item
        {
            public string Name
            {
                get;
                set;
            }

            public string Prefab { get; set; }

            public string Item_Type_Name
            {
                get;
                set;
            }

            public string Item_Name
            {
                get;
                set;
            }

            public string item_rarity { get; set; }

            public string item_description
            {
                get;
                set;
            }

            public string Item_Slot
            {
                get;
                set;
            }

            public PriceInfo Price_Info
            {
                get; set;
            }
        }

        public class PriceInfo
        {
            public string category_tags
            {
                get;
                set;
            }

            public int price
            {
                get;
                set;
            }
        }

        protected class SchemaResult
        {
            public Schema Items_game 
            {
                get;
                set;
            }
        }
    }

3 个答案:

答案 0 :(得分:2)

要将该字符串转换为有效的JSON,您只需稍微调整一下即可。

你可以用三个正则表达式替换来做到这一点:

var json = Regex.Replace(
               Regex.Replace(
                   Regex.Replace(
                       Regex.Replace(data, @"""(\r?\n\s*\{)", @""":$1"),
                       @"(})(\r?\n\s*"")", @"$1,$2"),
                    @"""(\r?\n\s*\"")", @""",$1"),
                 @"""\s+""", @""": """);

data包含您问题中显示的原始数据。然后我们执行以下转换:

  1. 我们在一行后面加上大括号
  2. 添加一个冒号
  3. 我们在引用
  4. 后面的行末尾添加逗号
  5. 我们在以花括号结尾的行尾添加一个逗号,后跟引号
  6. 我们在两个引号之间添加一个冒号

答案 1 :(得分:0)

如果准JSON与您的示例中一样规则,并且如果您不介意使用命令行工具将准JSON转换为JSON,则可以轻松使用awk或某些组合,例如sed和any-json。以下适用于您的示例(假设quasijson.txt是输入文件):

$ sed -e '/"/s/"/":/2' -e 's/}/},/' quasijson.txt |\
  sed -e '1s/^/{/' -e 's/" *$/",/' -e '$s/$/}/' |\
  any-json -format=json5

答案 2 :(得分:0)

我能够通过在Daniel提供的每个“}”+附加正则表达式代码之后添加逗号来解决问题。文本文件之后转换为JSON格式没有问题,也阅读。即使在“}”之后的所有地方添加了逗号 - 闭合大括号,它也可以。

感谢您帮助所有人。