Reputation: 188
I have the following content of a txt file, actually here is the original link: 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"
}
}
I am trying to convert this to CSV file so that I could easily convert CSV to JSON and do deserialization and populate my class with attributes and values from this textual file that is data value format.
I have the following code that is parsing the txt file:
Treenode rarities = root.Child["items_game"];
StreamWriter sw = new StreamWriter("items_game.csv");
sw.AutoFlush = true;
RecursiveReadSchema(rarities, sw, 0);
And
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);
}
}
My problem is the part of "static_attributes", they are not placed on proper place in CSV file so as its elements. Now if anybody knows a better way of converting this txt file to JSON directly I would go even for that solution but I didn't find anything online. For now, I am trying to parse this txt as treenode and then treenode to CSV file from there to JSON and from JSON to my class.
KVParser class that I used is from here: http://pastebin.com/C9t3y3H7
Thanks,
EDITED:
With this line:
SchemaResult schemaResult = JsonConvert.DeserializeObject<SchemaResult>(result);
Here is my class 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;
}
}
}
Upvotes: 0
Views: 423
Reputation: 188
I was able to resolve the problem by adding comma after every "}" + additional Regex code that Daniel provided. Textual file was after that converted into JSON format without problems and read also. Even though this added comma on all places after "}" - closed curly brace, it works.
Thanks for helping everyone.
Upvotes: 0
Reputation: 116790
If the quasi-JSON is as regular as in your example, and if you don't mind using command-line tools to convert the quasi-JSON to JSON, you could easily use awk, or some combination such as sed and any-json. The following works for your sample (assuming quasijson.txt is the input file):
$ sed -e '/"/s/"/":/2' -e 's/}/},/' quasijson.txt |\
sed -e '1s/^/{/' -e 's/" *$/",/' -e '$s/$/}/' |\
any-json -format=json5
Upvotes: 0
Reputation: 174329
To convert that string into valid JSON, you just need to tweak it a little bit.
You can do that with three regex replaces:
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
contains your original data as shown in your question. Then we perform the following transformations:
Upvotes: 2