Unity3D C# - 解析 JSON 在数据周围有一个额外的数组
Unity3D C# - Parse JSON that has an additional array around data
我陷入了一种情况,我的 json(来自我无法控制的来源)有一种什么都不做的“中间人”数组。
如果我修改 json 我可以得到值,但我很难过。
精简json示例
"geopoly": {
"type": "Polygon",
"coordinates": [
[
[
-74.7,
40.72
],
[
-74.73,
40.71
]
]
]
}
问题
如何使用 Unity JsonUtility.FromJson
将类似于此的 json 转换为 c# 对象?
当前尝试:用于解析的 C# 容器
[Serializable]
public struct GeoPoly
{
public string type;
public List<FloatList> coordinates;
}
[Serializable]
public class MiddleManList : List<FloatList> {}
[Serializable]
public class FloatList : List<float> {}
我可以正确看到“类型”,但“坐标”总是 [0]: Count = 0
或 null,所以我不确定如何解决这个问题。
你可以这样使用
var data = JsonUtility.FromJson<Root>(json);
添加根和坐标
[Serializable]
public class Root
{
public Geopoly geopoly {get; set;}
}
[Serializable]
public struct GeoPoly
{
public string type {get; set;}
public List<List<List<float>>> coordinates { get; set; }
}
并通过在 {}
中包装来修复 json
{
"geopoly": {
"type": "Polygon",
"coordinates": [
[
[
-74.7,
40.72
],
[
-74.73,
40.71
]
]
]
}
}
如果它不起作用仍然尝试删除 {get; set;}, 某些版本的 unity serializer 不知道如何序列化属性。
我陷入了一种情况,我的 json(来自我无法控制的来源)有一种什么都不做的“中间人”数组。
如果我修改 json 我可以得到值,但我很难过。
精简json示例
"geopoly": {
"type": "Polygon",
"coordinates": [
[
[
-74.7,
40.72
],
[
-74.73,
40.71
]
]
]
}
问题
如何使用 Unity JsonUtility.FromJson
将类似于此的 json 转换为 c# 对象?
当前尝试:用于解析的 C# 容器
[Serializable]
public struct GeoPoly
{
public string type;
public List<FloatList> coordinates;
}
[Serializable]
public class MiddleManList : List<FloatList> {}
[Serializable]
public class FloatList : List<float> {}
我可以正确看到“类型”,但“坐标”总是 [0]: Count = 0
或 null,所以我不确定如何解决这个问题。
你可以这样使用
var data = JsonUtility.FromJson<Root>(json);
添加根和坐标
[Serializable]
public class Root
{
public Geopoly geopoly {get; set;}
}
[Serializable]
public struct GeoPoly
{
public string type {get; set;}
public List<List<List<float>>> coordinates { get; set; }
}
并通过在 {}
中包装来修复 json{
"geopoly": {
"type": "Polygon",
"coordinates": [
[
[
-74.7,
40.72
],
[
-74.73,
40.71
]
]
]
}
}
如果它不起作用仍然尝试删除 {get; set;}, 某些版本的 unity serializer 不知道如何序列化属性。