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
Fixed by changing how I defined the json
Used SimpleJSON to make deserialize/read the JSON
Here you’re expecting an
int
:But what JSON value represents that
int
? This:That’s not an integer. That’s an array of arrays of arrays of doubles. Maybe you wanted this type instead?:
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.