- Home /
c# How to read a string from Json and convert it to an enum
Hi, very new and stupid programmer here. I'm attempting to make a simple RPG following this series link text and i wish to add WeaponTypes and ArmorTypes to my items, so i can equip them into Equipment slots further down the line, but i'm having trouble understanding how to read info and convert it.
Here's a copy of what i thought would work :
using UnityEngine;
using System.Collections;
using LitJson;
using System.Collections.Generic;
using System.IO;
public class ItemDatabase : MonoBehaviour {
private List<Item> database = new List<Item>();
JsonData itemData;
void Start()
{
//Item item = new Item(0, "Basic Short Sword", 5); create your own.
itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Items.json"));
ConstructItemDatabase();
Debug.Log(FetchItemByID(1).Description);
}
public Item FetchItemByID(int id)
{
for(int i=0; i < database.Count; i++) // Searches through the database for an item matching the ID you give it.
{
if(database[i].ID == id)
{
return database[i];
}
}
return null;
}
public Item FetchItemByName(string name)
{
for(int i = 0; i < database.Count; i++)
{
if(database[i].Title == name)
{
return database[i];
}
}
return null;
}
void ConstructItemDatabase()
{
// Having problems here for loading Enums
for (int i = 0; i < itemData.Count; i++)
{
database.Add(new Item((int)itemData[i]["id"], itemData[i]["title"].ToString(), itemData[i]["itemType"].ToString, itemData[i]["weaponType"]/* Nope */, itemData[i]["armorType"], (int)itemData[i]["value"], (int)itemData[i]["stats"]["damage"], (int)itemData[i]["stats"]["armor"], (int)itemData[i]["stats"]["block"], (int)itemData[i]["stats"]["dodge"], (int)itemData[i]["stats"]["speed"], itemData[i]["description"].ToString(), (bool)itemData[i]["isEquippable"], (bool)itemData[i]["stackable"], (bool)itemData[i]["consumable"], (string)itemData[i]["slug"]));
}
}
}
public class Item
{
public int ID { get; set; }
public string Title { get; set; }
public int Value { get; set; }
public int Damage { get; set; }
public int Armor { get; set; }
public int Block { get; set; }
public int Dodge { get; set; }
public int Speed { get; set; }
public string Description { get; set; }
public bool IsEquippable { get; set; }
public bool Stackable { get; set; }
public bool Consumable { get; set; }
public string Slug { get; set; }
public enum ItemType
{
Weapon,
Armor,
Consumable,
Misc,
Material,
Quest
}
public ItemType itemType { get; set; }
public enum WeaponType
{
Dagger,
ShortSword,
LongSword,
BroadSword,
Axe,
HeavyAxe,
Staff,
Spear,
Shield
}
public WeaponType weaponType { get; set; }
public enum ArmorType
{
Head,
Torso,
Legs,
Feet,
MainHand,
OffHand,
Ring,
Neck,
Trinket
}
public ArmorType armorType { get; set; }
public Sprite Sprite { get; set; }
public Item(int id, string title, string itmType, WeaponType wpnType, ArmorType armrType, int value, int damage, int armor, int block, int dodge, int speed, string desc, bool equippable, bool stackable, bool consumable, string slug)
{
//If i make Item(int id, string title, ItemType itmType... into string itmType i need to convert it to enum somehow?
//ItemType parsed_enum = System.Enum.Parse(typeof(ItemType), your_string); wtf do i do with this?
this.ID = id;
this.Title = title;
// ItemType itemtype = System.Enum.Parse(typeof(ItemType), itemType); nope...
this.weaponType = wpnType;
this.Value = value;
this.Damage = damage;
this.Armor = armor;
this.Block = block;
this.Dodge = dodge;
this.Speed = speed;
this.Description = desc;
this.IsEquippable = equippable;
this.Stackable = stackable;
this.Consumable = consumable;
this.Slug = slug;
this.Sprite = Resources.Load<Sprite>("Sprites/Items/" + slug);
}
public Item()
{
this.ID = -1;
}
}
Answer by Stewzor · Sep 01, 2016 at 08:33 AM
I managed to fix it after many stressful hours using:
public Item(int id, string title, string itmType, string weapnType, string armrType, int value, int damage, int armor, int block, int dodge, int speed, string desc, bool equippable, bool stackable, bool consumable, string slug)
{...
this.itemType = (ItemType)Enum.Parse(typeof(ItemType), itmType);
this.armorType = (ArmorType) Enum.Parse(typeof(ArmorType), armrType);
this.weaponType = (WeaponType)Enum.Parse(typeof(WeaponType), weapnType);
...}
And just casting (string) when constructing the database. I have no idea if this is the most efficient / best way of doing it but it seems to be working.
Answer by flaviusxvii · Aug 31, 2016 at 06:59 PM
Try something like:
Enum.Parse(WeaponType, itemData[i]["weaponType"])
Answer by natemiddleman · Sep 01, 2016 at 08:33 AM
This should help: http://www.boxheadproductions.com.au/deserializing-top-level-arrays-in-json-with-unity/
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Is there a reason not to use system.json.dll? 2 Answers
Enum has "Unknown resolve error" 1 Answer
Is it possible to executing object serialization into JSON asynchronously? 1 Answer