using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SimpleJSON;
public class StockManager : MonoBehaviour {
private string webString;
private CurrencyContainer container;
[SerializeField]private int currenciesToLoad;
void Start()
{
StartCoroutine(GetText());
}
void Update()
{
if (container != null)
{
Debug.Log (container);
}
else
{
Debug.Log ("null");
}
}
IEnumerator GetText()
{
using (WWW www = new WWW("https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD"))
{
yield return www;
if (www.error != null)
{
Debug.Log("Error is : " + www.error);
}
else
{
webString = "{ \"Arr\":" + www.text + "}";
container = JsonUtility.FromJson<CurrencyContainer> (webString);
}
}
}
[System.Serializable]
public class Datum
{
public int time;
public double close;
public double high;
public double low;
public double open;
public double volumefrom;
public double volumeto;
}
[System.Serializable]
public class ConversionType
{
public string type;
public string conversionSymbol;
}
[System.Serializable]
public class Example
{
public string Response;
public int Type;
public bool Aggregated;
public IList<Datum> Data;
public int TimeTo;
public int TimeFrom;
public bool FirstValueInArray;
public ConversionType ConversionType;
}
[System.Serializable]
public class CurrencyContainer
{
public Example[] Arr;
}
}
Okay, so this is the modified code. The problem is, i am getting an âUnexpected node type errorâ and i donât know how to fix it.
Ok, I think the problem is that your JSON data should look like (from json2csharp website):
public class Datum
{
public int time { get; set; }
public double close { get; set; }
public double high { get; set; }
public double low { get; set; }
public double open { get; set; }
public double volumefrom { get; set; }
public double volumeto { get; set; }
}
public class ConversionType
{
public string type { get; set; }
public string conversionSymbol { get; set; }
}
public class RootObject
{
public string Response { get; set; }
public int Type { get; set; }
public bool Aggregated { get; set; }
public List<Datum> Data { get; set; }
public int TimeTo { get; set; }
public int TimeFrom { get; set; }
public bool FirstValueInArray { get; set; }
public ConversionType ConversionType { get; set; }
}
So you should cast your JSON to RootObject because this is the type that is serialized. Youâre trying to cast it to CurrencyContainer and this is not the type of the json, because your web response is not an array of RootObjects.
So it appears the code being used is somewhat releated to something I shared in a previous thread:
In that previous thread the kind of json that was being parsed had a base type of an array. And since Unityâs JsonUtility doesnât support deserializing directly to an array, I suggested either using a different parser OR a hack fix was to append on some extra json around the returned json (since OP doesnât control the source of the data) to make it an object.
And thatâs what CurrencyContainer was used for. It just had a single field call âArrâ which contained all the Currency entries.
âŚ
This new json object is a completely different object. It needs to be formatted differently.
It appears @Syganek has written up the shape of this new json. So that should work (I didnât review it, so canât 100% confirm, but I have no reason to doubt it).