c# - 在 Unity C# 中使用 2D 数组解析 JSON

标签 c# json list unity-game-engine

我正在使用 Unity 和 Python,并通过 UDP 将数据发送到我的 Unity 客户端。我正在发送一个 JSON,该 JSON 已成功到达我的 Unity 应用程序。我不知道如何正确解析这个 JSON。我已经尝试过:

JsonUtility.FromJson<T>(...)

但结果不正确。我实现了一个包含数据类型的 [System.Serialized] 类

List<List<int>> landmarks;
List<double> eulerAngles;
List<List<double>> boxPoints;

并在我的构造函数中使用初始化这些

landmarks = new List<List<int>>(); 
...

每次我尝试打印结果时,所有变量都是空的。

我的 JSON 看起来像这样 -

{
    "eulerAngles": [6.2255, -15.8976, -0.5881], 
    "boxPoints": [
        [194.05965, 194.15782], 
        [182.35829, 189.05042], 
        [6 more...]
    ], 
    "landmarks": [
        [196, 225], 
        [197, 242], 
        [199, 258], 
        [65 more..]
     ]
}

感谢任何帮助。

最佳答案

您的 JSON 格式仍然很奇怪,我不明白您的数据结构。

为什么你把所有东西都存储在 List<int[]> 中而你得到的值显然是 float (或 double )值?

如果有的话,它可能应该是

[Serializable]
public class JsonData
{
    // here maybe int is ok at least from your data it seems so
    public List<int[]> landmarks = new List<int[]>();

    // for those you clearly want floats
    public List<float[]> eulerAngles = new List<float[]>();
    public List<float[]> boxPoints = new List<float[]>();
}

看看这个 JSON to c# converter !

但无论如何 Unity 的 JsonUtility好像不支持嵌套数组和列表的反序列化!我猜原因是,虽然其他原始数据类型的默认值与 null 不同,List数组是 null为默认值,并且必须在填充之前进行初始化。 JsonUtility似乎不支持这一点。我认为这与

Note that while it is possible to pass primitive types to this method, the results may not be what you expect; instead of serializing them directly, the method will attempt to serialize their public instance fields, producing an empty object as a result. Similarly, passing an array to this method will not produce a JSON array containing each element, but an object containing the public fields of the array object itself (of which there are none). To serialize the actual content of an array or primitive type, it is necessary to wrap it in a class or struct. (JsonUtility.ToJson)

a field of type T will have value default(T) - it will not be given any value specified as a field initializer, as the constructor for the object is not executed during deserialization (JsonUtility.FromJson)


但是,为什么还要将它们作为数组,其中每个数组都只有 一个 元素,而看起来(并且您的名字和标题表明)您宁愿存储始终为 2 和 3 的元组数字?

如果您可以更改 JSON 格式,那么您似乎更想要类似的内容

[Serializable]
public class JsonData
{
    // here maybe int is ok at least from your data it seems so
    public List<Vector2Int> landmarks = new List<Vector2Int>();

    // for those you clearly want floats
    public List<Vector3> eulerAngles = new List<Vector3>();
    public List<Vector2> boxPoints = new List<Vector2>();
}

你的 JSON 应该看起来像

{
    "eulerAngles": [
        {"x":6.2255, "y":-15.8976, "z":-0.5881},
        ...
    ], 
    "boxPoints": [
        {"x":194.05965, "y":194.15782}, 
        {"x":182.35829, "y":189.05042}, 
        ...
    ], 
    "landmarks": [
        {"x":196, "y":225}, 
        {"x":197, "y":242}, 
        {"x":199, "y":258}, 
        ...
     ]
}

查看 JSON 实际外观的最简单方法是获取 JsonData类并序列化一次,这样您就可以看到 Unity 期望的 JSON 内容。

var testData = new JsonData();
// fill it with example values
testData.eulerAngles = new List<Vector3>()
{
    new Vector3(6.2255f, -15.8976f, -0.5881f)
};
testData.boxPoints = new List<Vector2>()
{
    new Vector2(194.05965f, 194.15782f), 
    new Vector2(182.35829f, 189.05042f)
};
testData.landmarks = new List<Vector2Int>()
{
    new Vector2Int(196, 225),
    new Vector2Int(197, 242),
    new Vector2Int(199, 258)
}

var result = JsonUtility.ToJson(testData);

如果无法更改 JSON 格式,您可以查看 SimpleJSON它不会直接反序列化为类实例,而是反序列化为 JSONNode其中可以包含嵌套列表和数组,例如

var jsonData = JSON.Parse(yourJson);
var firstEuerAngles = jsonData["eulerAngles"][0];

关于c# - 在 Unity C# 中使用 2D 数组解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55229295/

相关文章:

jQuery比较并找出2个json数组之间的差异

c# - Silverlight:无法在文本框中输入俄语文本

json - gson和json有什么区别?

c# - PLINQ 异常

ios - 使用 Flickr API 时的 Alamofire AFError

arrays - 如何填写列表(类型)?

python - Python 中数百万字符串列表的内存使用情况

Excel:如何创建尊重一列的唯一元素列表?

c# - 在不使用硬 CPU 的情况下动态更改 HttpClient 中的代理

c# - 设置 Font.Color 时 Excel 2007 VSTO 插件异常