LitJson: unknown LitJson and TypeInitialization Exception, invalid tokens
Database.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using System.IO;
public class Database : MonoBehaviour
{
//have temporarily silenced all Mech variables and dependant methods to troubleshoot, because I have populated a JSON mech list and not yet put in its type constructors or properties.
public static List<Weapon> WeaponDatabase = JsonMapper.ToObject<List<Weapon>>(File.ReadAllText(Application.dataPath + "/Mechwarrior Remake/StreamingAssets/weapons.json"));
//public static List<Mech> MechDatabase = JsonMapper.ToObject<List<<Mech>>(File.ReadAllText(Application.dataPath + "/Mechwarrior Remake/Streaming Assets/mechs.json"));
public static List<Personnel> PersonnelDatabase = JsonMapper.ToObject<List<Personnel>>(File.ReadAllText(Application.dataPath + "/Mechwarrior Remake/Streaming Assets/personnel.json"));
//variables that pull down and reads item data from Json files and returns info by index
public static JsonData WeaponData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/Mechwarrior Remake/StreamingAssets/weapons.json"));
public static JsonData MechData;
public static JsonData PersonnelData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/Mechwarrior Remake/Streaming Assets/personnel.json"));
//matches database item ID to a searched integer then returns item.
public static Weapon W_FetchItemById(int id)
{
for (int i = 0; i < WeaponData.Count; i++)
{
if (WeaponDatabase[i].ID == id)
{
return WeaponDatabase[i];
}
Debug.Log("Weapon ID Fetcher could not find database info and failed to create Weapons");
}
Debug.Log("Weapon ID Fetcher failed to construct");
return null;
}
/*public static Mech M_FetchItemById(int id)
{
for (int i = 0; i < MechData.Count; i++)
{
if (MechDatabase[i].ID == id)
{
return MechDatabase[i];
}
Debug.Log("Mech ID Fetcher could not find database info and failed to create Mechs");
}
Debug.Log("Mech ID Fetcher failed to construct");
return null;
}*/
public static Personnel P_FetchItemById(int id)
{
for (int i = 0; i < PersonnelData.Count; i++)
{
if (PersonnelDatabase[i].ID == id)
{
return PersonnelDatabase[i];
}
Debug.Log("Personnel ID Fetcher could not find database info and failed to create Personnel");
}
Debug.Log("Personnel ID Fetcher failed to construct");
return null;
}
}
// item types
public class Weapon
{
public int ID { get; set; }
public string Title { get; set; }
public string Type { get; set;}
public int Cost { get; set;}
public int Ammo { get; set;}
public int Damage { get; set;}
public int Heat { get; set;}
public int Range { get; set;}
public int ReloadTime { get; set;}
public int Weight { get; set;}
public string Description { get; set;}
public bool Stackable { get; set;}
public string Slug { get; set;}
public Sprite Sprite { get; set;}
public Weapon(int id, string title, string type, int cost, int ammo)
{
this.ID = id;
this.Title = title;
this.Type = Type;
this.Cost = cost;
this.Ammo = ammo;
}
public Weapon()
{
this.ID = -1;
}
}
public class Mech
{
public int ID { get; set; }
public string Title { get; set; }
public Mech(int id, string title)
{
this.ID = id;
this.Title = title;
}
public Mech()
{
this.ID = -1;
}
}
public class Personnel
{
public int ID { get; set; }
public string Title { get; set; }
public Personnel(int id, string title)
{
this.ID = id;
this.Title = title;
}
public Personnel()
{
this.ID = -1;
}
}
weapons.json
[
{
"id": 0,
"title": "Machine Gun Array",
"type": "energy",
"cost": 100,
"stat":
{
"ammo": 10,
"damage": 1,
"heat": 1,
"range": 100,
"reloadtime": 1,
"weight": 1
}
"description": "a sample weapon",
"stackable": true,
"slug": "machine_gun_array"
}
]
WeaponInventory.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class WeaponInventory : MonoBehaviour
{
public static List<Weapon> WeaponInventoryStock = new List<Weapon>();
void WeaponInventoryPrint()
{
for (int i = 0; i < Database.WeaponDatabase.Count; i++)
{
WeaponInventoryStock.Add(new Weapon((int)Database.WeaponData[i]["id"], Database.WeaponData[i]["title"].ToString(), Database.WeaponData[i]["type"].ToString(),
(int)Database.WeaponData[i]["cost"], (int)Database.WeaponData[i]["ammo"]));
}
}
//Item UI
//variables for establishing the parents gameobjects under which on-UI items will stack
GameObject WeaponInventoryStack;
//open variables were you drag and drop prefabs for UI items
public GameObject GO_Weapon;
public GameObject Slot_Weapon;
//all lists and variables actually required to perform on-UI database construction
int WeaponInventorySlotCount;
public List<Weapon> ParsedWeapon = new List<Weapon>();
public List<GameObject> WeaponInventorySlot = new List<GameObject>();
void Start()
{
WeaponInventoryPrint();
//construct Inventory UI Weapons
WeaponInventorySlotCount = Database.WeaponDatabase.Count;
WeaponInventoryStack = GameObject.Find("InventoryWeaponsShoppingQueue").transform.gameObject;
for (int i = 0; i < WeaponInventorySlotCount; i++)
{
ParsedWeapon.Add(new Weapon());
WeaponInventorySlot.Add(Instantiate(Slot_Weapon));
WeaponInventorySlot[i].transform.SetParent(WeaponInventoryStack.transform);
AddWeapon(i);
}
GameObject.Find("InventoryWeaponsShoppingQueue").SetActive(false);
}
public void AddWeapon(int id)
{
Weapon WeaponBeingAdded = Database.W_FetchItemById(id);
if (ParsedWeapon[id].ID == -1)
{
ParsedWeapon[id] = WeaponBeingAdded;
GameObject WeaponUIObject = Instantiate(GO_Weapon);
WeaponUIObject.name = WeaponBeingAdded.Title;
WeaponUIObject.transform.SetParent(WeaponInventorySlot[id].transform);
WeaponUIObject.GetComponent<Image>();
}
}
}
I am having issues with what my guess would be serializing data between my JSON files and my actual scripts that are dependant on them to assemble lists. I've looked at roundabout ways of parsing the JSON data in for loops and what not (perhaps not hard enough), but no matter what I try and after checking my JSON data for misplaced commas or type errors, the same error keeps appearing. It doesn't seem to be a typical "Read() error" and I've found nothing online for support with a token 34 error. I'm just gonna need help from someone who knows about the LitJson plugin way more than I do, to even understand where the problem is let alone fix it.
Thanks in advance, sorry for the novel...
Your answer
Follow this Question
Related Questions
LitJson: unknown JSON and TypeInstantiation Exceptions, invalid tokens 0 Answers
Spawn unique objects from the array 1 Answer
get the total valor float inside a Gameobject from a List 1 Answer
Can someone help me please? 0 Answers
DLLNotFoundException when trying to import Firebase project 2 Answers