0

What I'm trying to do is add the data I have saved in a litjson file to a list, and in the correct format. So I can call it in the game later on, as icons. Here's the code, so it should be easy to recreate to test the problem I'm having.

JsonIcons class:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class JsonIcons {

    public string IconName;//Shows the icon Name in the list
    public int IconID;// Shows the Icon ID in the list
    public Sprite AssignIcon;

    public JsonIcons(string Name, int ID )
    {
        IconName = Name;
        IconID = ID;
    }

    public JsonIcons()
    {
    }
}

JsonTest class:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using System.IO;

//This class that does the saving
public class JsonTest : MonoBehaviour {

    public List<JsonIcons> JIcon = new List<JsonIcons>();
    public JsonData JCD;

    protected JsonIcons KZ, TestTK;

    public void Start()
    {
        TestTK = new JsonIcons("Kagami", 40);
        KZ = new JsonIcons("Magic", 0);

        JIcon.Add(TestTK); //Add things to the list to be save
        JIcon.Add(KZ);

        JCD = JsonMapper.ToJson(JIcon);
        //This is where I saved the things inside the JIcon list to a Json file
        File.WriteAllText(Application.dataPath + "/JsonSaveTest.json", JCD.ToString());
        //Debug.Log(JCD);
    }
}

JsonReadTest class:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using LitJson;

public class JsonReadTest : MonoBehaviour {

    public List<JsonIcons> ReadSJ = new List<JsonIcons>();

    private string JString;
    public JsonData IconData;

    // Use this for initialization
    void Start () 
    {
        //Trying to get this file to load in the correct format inside the ReadSJ list
        JString = File.ReadAllText(Application.dataPath + "/JsonFiles/JsonSaveTest.json");
        IconData = JsonMapper.ToObject(JString);
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
War Mind
  • 1
  • 1
  • 1
    I editted your title. Please see, ["Should questions include “tags” in their titles?"](http://meta.stackoverflow.com/questions/19190/), where the consensus is "no, they should not"." – Luizgrs Sep 30 '15 at 16:11
  • Alright that's cool. sorry about that, Didn't know that. – War Mind Sep 30 '15 at 17:47

1 Answers1

0

You could either create a class called JsonIconList, which would only contain a List (by the way you should rename JsonIcons to JsonIcon as it represents a single item). Then you can easily deserialize it with JsonMapper class.

Another way would be to have a generic wrapper class like this

[Serializable]
private class ListWrapper<T>
{
    public List<T> Items;
}

Then create a helper method

public static class JsonHelper
{
    public static List<T> ToList<T>(string json)
    {
        ListWrapper<T> wrapper = JsonMapper.ToObject<ListWrapper<T>>(json);
        return wrapper.Items;
    }
}

And use it like this

List<JsonIcon> icons = JsonHelper.ToList<JsonIcon>(jsonString);
ThunderDev
  • 1,118
  • 2
  • 19
  • 38