Can't convert object containing array to Json

I have a class that looks like this -

    [Serializable]
    public class JNodeList
    {
        public JsonNode[] nodes;

        public JNodeList(JsonNode[] array) { nodes = array; }
    }

JsonNode looks like this -

    public class JsonNode {
        public string Name;
        public Vector3 Location;
        public Vector3 Rotation;
        public string UserData;

        public JsonNode(Node node) {
            Name = node.name;
            Location = node.transform.position;
            Rotation = node.transform.eulerAngles;
            UserData = node.UserData;
        }

        public JsonNode(GameObject obj) {
            Name = obj.name;
            Location = obj.transform.position;
            Rotation = obj.transform.eulerAngles;
            UserData = "";
        }
    }

Yet in this code - (where hits is a list of "RaycastHit"s)

        var nodes = hits.Select(x => new JsonNode(x.transform.gameObject)).ToArray();
        var jsonNodes = JsonUtility.ToJson(new JNodeList(nodes), true);

“jsonNodes” comes out as “{}”.

nodes is definitely populated so what am I doing wrong?

Since you’re doing arrays, this might be relevant. (and if it doesn’t apply, sorry. I use json.net myself). I just remember someone else mentioned arrays and jsonutility don’t work, but I haven’t really looked into the details myself.

Thanks but the solution in that case is to have the array as a member on an object and convert that object to Json which is exactly what I’m doing here

Fixed. For anyone wondering, JsonNode needed to also be serializable.