简体   繁体   English

使用 UnityEngine.JsonUtility 反序列化 Unity3d 中的来自 InfluxDB REST 的 JSON

[英]Deserialize JSON from InfluxDB REST in Unity3d with UnityEngine.JsonUtility

Im having problem deserielizing the JSON response from influxDB in C# using Unity3d's JsonUtility class.我在使用 Unity3d 的 JsonUtility class 对 C# 中的 influxDB 的 JSON 响应进行反序列化时遇到问题。

I used json2csharp.com to generate my class.我使用 json2csharp.com 来生成我的 class。 The problem seems to be that there is a List with a List of objects in the result JSON string.问题似乎是在结果 JSON 字符串中有一个带有对象列表的列表。

this is an example JSON result from InfluxDB:这是来自 InfluxDB 的示例 JSON 结果:

{
    "results": [
        {
            "statement_id": 0,
            "series": [
                {
                    "name": "cpu_load_short",
                    "columns": [
                        "time",
                        "value"
                    ],
                    "values": [
                        [
                            "2015-01-29T21:55:43.702900257Z",
                            2
                        ],
                        [
                            "2015-01-29T21:55:43.702900257Z",
                            0.55
                        ],
                        [
                            "2015-06-11T20:46:02Z",
                            0.64
                        ]
                    ]
                }
            ]
        }
    ]
}

And this is the generated class from json2csharp:这是从 json2csharp 生成的 class :

public class Series
{
    public string name { get; set; }
    public List<string> columns { get; set; }
    public List<List<object>> values { get; set; }
}

public class Result
{
    public int statement_id { get; set; }
    public List<Series> series { get; set; }
}

public class RootObject
{
    public List<Result> results { get; set; }
}

I Just can't figure out how to handle the List<List<object>> in the values field.我只是不知道如何处理值字段中的List<List<object>>

When running the program I get this log output (this is not the example JSON but an actual result from my DB, the formatting is the same though):运行程序时,我收到此日志 output (这不是示例 JSON 而是来自我的数据库的实际结果,但格式相同):

InfluxResult (
    (List`1) results = List`1<Result> [
        Result (
            (Int32) statement_id = 0;
            (List`1) series = List`1<Series> [
                Series (
                    (String) name = "gf_hallway_temperature";
                    (List`1) columns = List`1<String> [
                        "time",
                        "value",
                    ];
                    (List`1) values = Null;
                ),
            ];
        ),
    ];
)

It parses one level deeper if I do:如果我这样做,它会更深一层地解析:

[Serializable]
public class Series
{
    public string name;// { get; set; }
    public List<string> columns; // { get; set; }
    public List<ValueSet> values; // { get; set; }
}

[Serializable]
public class ValueSet
{
    public List<object> values;
}

But that obviously just parses the values as a list of empty objects.但这显然只是将值解析为空对象的列表。 But they correspond to the correct count of the list of "valuesets".但它们对应于“值集”列表的正确计数。

I am a hobby-level developer so I might miss something fundamental here.我是一个爱好级别的开发人员,所以我可能会错过一些基本的东西。 I guess the json2csharp generator fails becouse of the nested lists?我猜 json2csharp 生成器由于嵌套列表而失败? But how should this be handled, is it possible with UnityEngine.JsonUtility?但是应该如何处理,UnityEngine.JsonUtility 可以吗? Also, the "valueset" varies depending on how the DB is setup so the list of columns represent the list of values and the type of each field in the row in series, if Im making any sense, so i guess there have to be some sort of generic object and I'll handle the problems that generates later on.此外,“值集”根据数据库的设置方式而有所不同,因此列列表表示值列表和行中每个字段的类型,如果我有任何意义的话,所以我想必须有一些一种通用的 object,我将处理稍后产生的问题。

[
                        [
                            "2015-01-29T21:55:43.702900257Z",
                            2
                        ],
                        [
                            "2015-01-29T21:55:43.702900257Z",
                            0.55
                        ],
                        [
                            "2015-06-11T20:46:02Z",
                            0.64
                        ]
                    ]

cannot be deserialized by JSONUtility .不能被JSONUtility反序列化。 Use a dedicated library like Newtonsoft JSON to iterate through a "Json Object" instead.使用像 Newtonsoft JSON 这样的专用库来迭代“Json 对象”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM