I am having trouble reading and writing multiple records from and to the json file. Before I added the "Items" array and added a second record it loaded the file fine. My problem is I am trying to load multiple items for the players inventory. When I try to do this it loads only the one item and says 'Additional text encountered after finished reading JSON content:' and it points to the line number in the json file which is ending the first item. Also when trying to save using a for loop it gives me an error saying that the property name already exists, so I tried making the whole thing an array but this didn't work either. I would also like to point out that this version of Json is different than some other versions I have seen so it is difficult to find information on this. I am using the Json from the Unity asset store. Any help would be appreciated.
{
"Items":
[
{
"Id": 0,
"Title": "Test0",
"Value": 15,
"Description": "TESTing0",
"Stackable": true,
"Rarity": 1,
"Slug": "TEST0",
"Stats":
[
{
"Power": 9,
"Defense" : 7,
"Vitality": 3
}
],
"Id": 1,
"Title": "Test1",
"Value": 16,
"Description": "TESTing1",
"Stackable": false,
"Rarity": 1,
"Slug": "TEST1",
"Stats":
[
{
"Power": 9,
"Defense" : 7,
"Vitality": 3
}
]
}
]
}
Here is my code so far. My save and load functions are completely different because I have been trying different things to get this working the way I intended. Right now my Load function is trying to load the array of "Items": Which is actually multiple records. For some reason this is not working for me and I get an error message saying "Accessed JObject values with invalid key value: 0. Object property name expected." I have also tried this without the array "Items": but then it reads one record and says 'Additional text encountered after finished reading JSON content:'
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
public class PlayerDatabase : MonoBehaviour
{
private List<Item> database = new List<Item>();
public class Item
{
public int Id { set; get; }
public string Title { set; get; }
public int Value { set; get; }
public int Power { set; get; }
public int Defense { set; get; }
public int Vitality { set; get; }
public string Description { set; get; }
public bool Stackable { set; get; }
public int Rarity { set; get; }
public string Slug { set; get; }
public Item(int id, string title, int value, int power, int defense, int vitality, string description, bool stackable, int rarity, string slug)
{
this.Id = id;
this.Title = title;
this.Value = value;
this.Power = power;
this.Defense = defense;
this.Vitality = vitality;
this.Description = description;
this.Stackable = stackable;
this.Rarity = rarity;
this.Slug = slug;
}
}
void Save()
{
JObject playerJson = new JObject();
for (int i = 0; i < 1; i++)
{
playerJson.Add("Id", i);
playerJson.Add("Title", Title);
playerJson.Add("Value", Value);
playerJson.Add("Description", Description);
playerJson.Add("Stackable", Stackable);
playerJson.Add("Rarity", Rarity);
playerJson.Add("Slug", Slug);
JArray jaStats = new JArray();
jaStats.Add(Power);
jaStats.Add(Defense);
jaStats.Add(Vitality);
playerJson.Add("Stats", jaStats);
string path = Application.dataPath + "/Inventory System/StreamingAssets/Player.Json";
File.WriteAllText(path, playerJson.ToString());
}
}
void Load()
{
string path = Application.dataPath + "/Inventory System/StreamingAssets/Player.Json";
string jsonString = File.ReadAllText(path);
JObject itemJson = JObject.Parse(jsonString);
//Load values
for (int i = 0; i < itemJson.Count; i++)
{
database.Add(new Item((int)itemJson[i]["Items"]["Id"], (string)itemJson[i]["Items"]["Title"], (int)itemJson[i]["Items"]["Value"],
(int)itemJson[i]["Items"]["Stats"]["Power"], (int)itemJson[i]["Items"]["Stats"]["Defense"], (int)itemJson[i]["Items"]["Stats"]["Vitality"],
(string)itemJson[i]["Items"]["Description"], (bool)itemJson[i]["Items"]["Stackable"], (int)itemJson[i]["Items"]["Rarity"], (string)itemJson[i]["Items"]["Slug"]));
}
Debug.Log(database[0].Power);
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.S)) Save();
if (Input.GetKeyDown(KeyCode.L)) Load();
}
}