2

I have the following class, which is serializable and only has strings as fields:

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

[System.Serializable]
public class Cabeza
{

    string id, urlOBJ, urlTextura, pathOBJbajado, pathTexturaBajada;

    public string Id { get; set; }
    public string UrlOBJ { get; set; }
    public string UrlTextura { get; set; }
    public string PathOBJbajado { get; set; }
    public string PathTexturaBajada { get; set; }


    public Cabeza (string nuevoId)
    {
        Id = nuevoId;
        UrlOBJ =  nuevoId +".obj";
        UrlTextura =  nuevoId + ".png";

    }


}

As far I know it should be possible to obtain a JSON from it...However, JsonUtility.ToJson() returns just { }. How is this possible? What am I missing?

3
  • I think it is serializing your fields, which do not have a value, and therefor not showing up in the serialization result. Commented Feb 22, 2016 at 15:57
  • Can you show the code where you call ToJson(...)? Commented Feb 22, 2016 at 15:58
  • 1
    @Maarten UrlOBJ ad UrlTextura should have values. Commented Feb 22, 2016 at 16:00

1 Answer 1

5

The documentation mentions (but doesn't make clear) that .ToJson() serializes fields, not properties.

I think the following code would work as you intend:

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

[System.Serializable]
public class Cabeza
{
    public string Id;
    public string UrlOBJ;
    public string UrlTextura;
    public string PathOBJbajado;
    public string PathTexturaBajada;

    public Cabeza (string nuevoId)
    {
        Id = nuevoId;
        UrlOBJ =  nuevoId +".obj";
        UrlTextura =  nuevoId + ".png";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.