我有一个txt文件的内容如下,其实这里是原文链接: http://cdn.dota2.com/apps/570/scripts/items/items_game.9e449667cd3ba55c9c545c2a4a1825da541057b0.txt将文本转换为CSV对JSON序列化
"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;
}
}
}
如果JSON是你想要的,为什么要将文件转换为CSV?据我所看到的文本文件几乎是JSON。只有'钥匙'的行是一个对象。这条线后面跟着一条花括号。之后,一个或多个属性(两个'键')或对象(再次一个'键')或一个对象结束(关闭大括号)。 – venerik
我试着直接使用这个txt文件进行JSON反序列化,但每次失败。原因如下:“无法将System.String强制转换或转换为SteamTrade.Schema + SchemaResult。”您可以在编辑的问题中看到我的SchemaResult类。 –
当然,它失败了。这是无效的JSON。所以你必须先将它转换成有效的JSON。但不是将其转换为CSV然后转换为JSON,我建议立即将其转换为JSON。 – venerik