简体   繁体   中英

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.

I used json2csharp.com to generate my class. The problem seems to be that there is a List with a List of objects in the result JSON string.

this is an example JSON result from InfluxDB:

{
    "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:

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.

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):

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? But how should this be handled, is it possible with 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.

[
                        [
                            "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 . Use a dedicated library like Newtonsoft JSON to iterate through a "Json Object" instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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