1

I need to save multiple player's data. I am doing it by making an array of PlayersInfo class and trying to convert the array into JSON. here is my code

  PlayerInfo[] allPlayersArray = new PlayerInfo[1];

  allPlayersArray[0] = new PlayerInfo();
  allPlayersArray[0].playerName = "name 0";

  string allPlayersArrayJson = JsonUtility.ToJson(allPlayersArray);
  print(allPlayersArrayJson);
  PlayerPrefs.SetString("allPlayersArrayJson", allPlayersArrayJson);

  string newJson = PlayerPrefs.GetString("allPlayersArrayJson");
  print(newJson);

  PlayerInfo[] newArray = new PlayerInfo[1];

  newArray = JsonUtility.FromJson<PlayerInfo[]>(newJson);

  print(newArray[0].playerName);

First two print statements returns "{}" and 3rd one gives null reference error. TIA

Affan Shahab
  • 838
  • 3
  • 17
  • 39
  • Unity Json Serializer does not support this directly. You need a helper class for it to work. I think that this is a duplicate question. Here is your answer http://stackoverflow.com/a/36244111/3785314 Start reading from where it says **2.MULTIPLE DATA(ARRAY JSON)** – Programmer Jul 15 '16 at 07:32
  • Thanks @Programmer, Can you please help me how I actually use it? I just copied the helper class now I wonder how I use it to convert my arrays? Please note I need to modify that array further. – Affan Shahab Jul 15 '16 at 12:04
  • The link to the answer I posted shows you steps to do that. Scroll down and try to follow them. If there is a problem then let me know. I will then post a solution. You must tell me what you tried before I post an answer. – Programmer Jul 15 '16 at 12:09
  • Here is what I tried. 'PlayerInfo[] firstArray = new PlayerInfo[1]; firstArray[0].playerName = "Name1"; string firstJSON = JsonHelper.ToJson(firstArray); PlayerPrefs.SetString("playerInfo", firstJSON); string firstString = PlayerPrefs.GetString("playerInfo"); PlayerInfo[] secondArray = new PlayerInfo[1]; secondArray = JsonHelper.FromJson(firstString); secondArray[0].playerName = "Name2"; string secondString = JsonHelper.ToJson(secondArray); PlayerPrefs.SetString("playerInfo", secondString); string thirdString = PlayerPrefs.GetString("playerInfo"); – Affan Shahab Jul 15 '16 at 12:20
  • But it gives null reference error on 2nd statement. – Affan Shahab Jul 15 '16 at 12:22
  • Ok Making an answer for this specific post. – Programmer Jul 15 '16 at 12:26
  • Thank you very much, waiting for it – Affan Shahab Jul 15 '16 at 12:31
  • Your problem is a little-bit different and does not require some steps on that I posted.I tested the solution I posted and It works for me. Try it. If that's not working for you, please update to Unity 5.4. – Programmer Jul 15 '16 at 12:57

4 Answers4

2

Like I said in my comment, there is no direct support. Helper class is needed. This is only reason I am making this answer is because you are still having problems even after reading the link I provided.

Create a new script called JsonHelper. Copy and paste the code below inside it.

using UnityEngine;
using System.Collections;
using System;

public class JsonHelper
{

    public static T[] FromJson<T>(string json)
    {
        Wrapper<T> wrapper = UnityEngine.JsonUtility.FromJson<Wrapper<T>>(json);
        return wrapper.Items;
    }

    public static string ToJson<T>(T[] array)
    {
        Wrapper<T> wrapper = new Wrapper<T>();
        wrapper.Items = array;
        return UnityEngine.JsonUtility.ToJson(wrapper);
    }

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

The code in your question should now work. All you have to do is to replace all JsonUtility words with JsonHelper. I did that for you below:

void Start()
{
    PlayerInfo[] allPlayersArray = new PlayerInfo[1];

    allPlayersArray[0] = new PlayerInfo();
    allPlayersArray[0].playerName = "name 0";

    string allPlayersArrayJson = JsonHelper.ToJson(allPlayersArray);
    print(allPlayersArrayJson);
    PlayerPrefs.SetString("allPlayersArrayJson", allPlayersArrayJson);

    string newJson = PlayerPrefs.GetString("allPlayersArrayJson");
    print(newJson);

    PlayerInfo[] newArray = new PlayerInfo[1];

    newArray = JsonHelper.FromJson<PlayerInfo>(newJson);

    print(newArray[0].playerName);
}
Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
0

Based on the JsonUtility documentation, naked arrays are not supported. Put the array inside a class.

Internally, this method uses the Unity serializer; therefore the object you pass in must be supported by the serializer: it must be a MonoBehaviour, ScriptableObject, or plain class/struct with the Serializable attribute applied.

In general, you'll use this to serialize MonoBehaviour objects, or a custom class/struct with the Serializable attribute.

piojo
  • 6,351
  • 1
  • 26
  • 36
0

YES! It is not supported currently but there is always a work around. Use this class :

public static class JsonHelper
{
    public static T[] getJsonArray<T>(string json)
    {
        try
        {
            string newJson = "{ \"array\": " + json + "}";
            Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>> (newJson);
            return wrapper.array;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    [Serializable]
    private class Wrapper<T>
    {
        public T[] array = null;
    }
}
Umair M
  • 10,298
  • 6
  • 42
  • 74
0

I would use Json.NET

PlayerInfo[] allPlayersArray = new PlayerInfo[] { p1, p2, p3 };

string json = JsonConvert.SerializeObject(allPlayersArray);

More On SerializeObject

(Code example not yet tested)

Olian04
  • 6,480
  • 2
  • 27
  • 54