我有一個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