0

I really need some help with desereliazing a JSON.

Here is my JSON : https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD

Here is the code I have so far :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;

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.Arr);
    } 
    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 = JsonConvert.DeserializeObject<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;
}

}

The error I get is : JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'StockManager+Example[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

I have no idea how to fix and any help is really appreciated. Thanks a lot.

1
  • 2
    Can you post your Json string? Commented Jan 31, 2018 at 9:31

1 Answer 1

1

You have "one level to much" in your object structure, as the given JSON is only on "item" of your type Example. Try the following:

var item = JsonConvert.DeserializeObject<Example>(www.text);

See it HERE in action.

Sign up to request clarification or add additional context in comments.

4 Comments

That will do it. This line webString = "{ \"Arr\":" + www.text + "}"; wasn't doing any good either.
Also, how would I go about printing each value in Data ? For example if I print data(0) i should get {"time":1514764800,"close":13444.88,"high":13921.53,"low":12877.67,"open":13850.49,"volumefrom":78425.21,"volumeto":1057521524.42} , data(1) should print : "time":1514851200,"close":14754.13,"high":15306.13,"low":12934.16,"open":13444.88,"volumefrom":137732.17,"volumeto":1956783037.35. Well, I know it doesn't work like that because Data is an array with only one element, but how could I achieve what I want? Thanks a lot again.
@weaverbeaver You can access them by item.Data[index], so e.g. item.Data[0]. (I updated the fiddle to dump data(0) and data(1))
Yeah, I was using item.Data(index) instead of item.Data[index]. Thanks a lot again !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.