[英]Deserializing JSON array into C# object
我確實需要一些反序列化JSON的幫助。
這是我的JSON: https : //min-api.cryptocompare.com/data/histoday? fsym = BTC & tsym =USD
這是我到目前為止的代碼:
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;
}
}
我得到的錯誤是:JsonSerializationException:無法將當前JSON對象(例如{“ name”:“ value”})反序列化為類型'StockManager + Example []',因為該類型需要JSON數組(例如[1,2,3 ])正確反序列化。
我不知道如何解決,非常感謝您的幫助。 非常感謝。
您的對象結構中有“一個級別到一個級別”,因為給定的JSON僅位於類型Example
“ item”上。 請嘗試以下操作:
var item = JsonConvert.DeserializeObject<Example>(www.text);
在這里看到它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.