skip to Main Content

Here is an example of my JSON data, it is automatically generated in a Python code.

{
  "class_labels":[
    [
      [
        0.0,
        0.0,
        1.0,
      ]
    ]
  ],
  "translations":[
    [
      [
        1.0,
        2.0,
        3.0,
      ],
      [
        3.0,
        2.0,
        1.0,
      ],
      [
        0.0,
        0.0,
        0.0,
      ]
    ]
  ],
  "scales":[
    [
      [
        1.0,
        2.0,
        3.0,
      ],
      [
        3.0,
        2.0,
        1.0,
      ],
      [
        0.0,
        0.0,
        0.0,
      ]
    ]
  ],
  "angles":[
    [
      [
        0.0,
      ],
      [
        0.0,
      ],
      [
        5.0,
      ]
    ]
  ],

It is supposed to be valid JSON but I can only get 0 where I try to Debug.Log() all of the items from the JSON. When I tried to Debug.Log() the entire JSON text (my pyOutput file), everything did show up.

Since this is the first time I have seen a JSON structure like this, I don’t know how to access each array.

This is my Unity C# code, int was corrected and edited into IEnumerable<IEnumerable<IEnumerable<double>>>.

public TextAsset pyOutput;
    public static class JSONReader
    {
        public static ModelInfo GetJSON(TextAsset pyOutput)
        {
            ModelInfo aModelInfo = JsonUtility.FromJson<ModelInfo>(pyOutput.text);
            return aModelInfo;
        }
    }

    private void Start()
    {
        Debug.Log(JSONReader.GetJSON(pyOutput));
    }

    [System.Serializable]
    public class ModelInfo
    {
        public IEnumerable<IEnumerable<IEnumerable<double>>> class_labels;
        public IEnumerable<IEnumerable<IEnumerable<double>>> translations;
        public IEnumerable<IEnumerable<IEnumerable<double>>> sizes;
        public IEnumerable<IEnumerable<IEnumerable<double>>> angles;
    }

The code is now returning null instead of 0, is it actually outputting the array but I’m just printing it wrongly to console?

2

Answers


  1. Chosen as BEST ANSWER

    Fixed by changing how I defined the json

    [System.Serializable]
    public class Params
        {
            public float[][][] class_labels = new float[0][][];
            public float[][][] translations = new float[0][][];
            public float[][][] sizes = new float[0][][];
            public float[][][] angles = new float[0][][];
        }
    
    [System.Serializable]
        public class Info
        {
            public Params Params;
        }
    

    Used SimpleJSON to make deserialize/read the JSON

    jsonString = File.ReadAllText(path);
            data = JSON.Parse(jsonString);
    
    foreach (JSONNode pos in data["translations"]) // translation
            {
                for (int i = 0; i < pos.Count; i++)
                {
                    for (int k = 0; k < pos[i].Count; k += 3)
                    {
                        posX.Add(pos[i][k].AsFloat);
                    }
                }
            }
    

  2. Here you’re expecting an int:

    public int class_labels;
    

    But what JSON value represents that int? This:

    [
      [
        [
          0.0,
          0.0,
          1.0,
        ]
      ]
    ]
    

    That’s not an integer. That’s an array of arrays of arrays of doubles. Maybe you wanted this type instead?:

    public IEnumerable<IEnumerable<IEnumerable<double>>> class_labels;
    

    If the property should be a single integer value (or should be anything other than what it currently is) then that would need to be corrected at the source of the data. But the data you have now is simply not an integer.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search