Hey everyone! I’ve been trying to get the data from API in a form of json and get some values from it. The json looks like this:
[{
"timestamp": "2022-07-07 12:50:44",
"id": 11222290496,
"location": {
"exact_location": 0,
"id": 60603,
"country": "DE",
"latitude": "52.548",
"indoor": 0,
"altitude": "55.3",
"longitude": "13.42"
},
"sensor": {
"sensor_type": {
"id": 14,
"manufacturer": "Nova Fitness",
"name": "SDS011"
},
"id": 72573,
"pin": "1"
},
"sensordatavalues": [{
"id": 24949825211,
"value": "6.75",
"value_type": "P1"
}, {
"id": 24949825213,
"value": "4.28",
"value_type": "P2"
}
],
"sampling_rate": null
}, {
"timestamp": "2022-07-07 12:48:17",
"id": 11222265122,
"location": {
"exact_location": 0,
"id": 60603,
"country": "DE",
"latitude": "52.548",
"indoor": 0,
"altitude": "55.3",
"longitude": "13.42"
},
"sensor": {
"sensor_type": {
"id": 14,
"manufacturer": "Nova Fitness",
"name": "SDS011"
},
"id": 72573,
"pin": "1"
},
"sensordatavalues": [{
"id": 24949766720,
"value": "5.25",
"value_type": "P1"
}, {
"id": 24949766738,
"value": "3.80",
"value_type": "P2"
}
],
"sampling_rate": null
}
]
The C# script for this json:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class Root
{
public Root2[] root2;
public List<Root> root;
}
[Serializable]
public class Root2
{
public Location location;
public Sensor sensor;
public object sampling_rate;
public string timestamp;
public object id;
public List<Sensordatavalue> sensordatavalues;
}
[Serializable]
public class Location
{
public string country;
public string latitude;
public int exact_location;
public int id;
public int indoor;
public string altitude;
public string longitude;
}
[Serializable]
public class Sensor
{
public SensorType sensor_type;
public string pin;
public int id;
}
[Serializable]
public class Sensordatavalue
{
public string value_type;
public string value;
public object id;
}
[Serializable]
public class SensorType
{
public string manufacturer;
public string name;
public int id;
}
And the script to get the API response and values from json:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class jsonController : MonoBehaviour
{
public string jsonURL;
private Root jsnData;
// Start is called before the first frame update
void Start()
{
//processJsonData(jsonURL);
StartCoroutine(getData());
}
IEnumerator getData()
{
Debug.Log("Processing...");
WWW _www = new WWW(jsonURL);
yield return _www;
if(_www.error == null)
{
processJsonData("{\"data\":" + _www.text + "}");
}
else
{
Debug.Log("Something's wrong");
}
}
private void processJsonData(string _url)
{
Root2 jsnData = JsonUtility.FromJson<Root2>(_url);
Debug.Log("Json dat: " + jsnData.id);
foreach(Root2 item in jsnData.data)
{
Debug.Log("id: " + item.id);
}
}
}
The issue is that jsnData.data in foreach is highlighted and the VS shows error “Root2 does not contain a definition for data”
I tried implementing the method (foreach) from this question:
Thank you in advance