I am trying to create an inventory system using a json file. Basically, I have my json file:
[
{
"name":"Potion",
"id":1,
"balance":5
},
{
"name":"Neutral Bomb",
"id":2,
"balance": 4
}
]
And I have my c# file:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using LitJson;
public class Item
{
public string Name { get; set; }
public int Id { get; set; }
public int Balance { get; set; }
public Item(string name1, int id1, int balance1) {
Name = name1;
Id = id1;
Balance = balance1;
}
}
public class InventoryAttempt : MonoBehaviour
{
public static void BalanceChange(string filePath, int ID, int change)
{
string jsonString = File.ReadAllText(Application.dataPath + filePath);
List<Item> itemList = JsonMapper.ToObject<List<Item>>(jsonString);
itemList [ID].Balance += change;
jsonString = JsonMapper.ToJson (itemList).ToString();
File.WriteAllText (Application.dataPath + filePath, jsonString);
}
void Start()
{
BalanceChange ("/Scripts/inventory.json", 1, 1);
}
}
In my c# file I want to access a single item, lets say the neutral bomb item. How can I go in and edit the balance of the neutral bomb? I want to use the Item ID to tell which object to edit. I want to create a list where each element is one of the items that are stored in the Json file, but I'm not sure how to separate them. What should I do?
MissingMethodException: Method not found: 'Default constructor not found...ctor() of Item'.
System.Activator.CreateInstance (System.Type type, Boolean nonPublic) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Activator.cs:368)
System.Activator.CreateInstance (System.Type type) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Activator.cs:254)
LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader)
LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader)
LitJson.JsonMapper.ToObject[List`1] (System.String json)
InventoryAttempt.BalanceChange (System.String filePath, Int32 ID, Int32 change) (at Assets/Scripts/InventoryAttempt.cs:26)
InventoryAttempt.Start () (at Assets/Scripts/InventoryAttempt.cs:34)
First of all, the variable names in your Json does not match the ones in your
Item
class. The ones in yourItem
class are capitalized.Use Unity's
JsonUtility
.Secondly, get the
JsonHelper
class from this post. It allows Unity'sJsonUtility
to serialize and deserialize Json Array.Do not generate Json by hand in the future since you will run into problems like this. Use the Json class and a simple function to do that. For example, this:
Will generate this:
No error. That is valid json.
Solution:
InventoryAttempt
class:Your
Item
classItem
.Test script:
Note:
I removed get/set property stuff. Please remove these as they must be removed for this to work. Also,
[Serializable]
is added to the top of theItem
class. Please add that as-well. I suggest you copy and replace the classes directly before making the "it doesn't work" comment under the question...