Home > Blockchain >  cant seem to Deserialize to strongly typed list object from array
cant seem to Deserialize to strongly typed list object from array

Time:02-07

I'm trying to deserialize use the following Json and classes:

List<Root> OBJTEST = (List<Root>)Newtonsoft.Json.JsonConvert.DeserializeObject(response, typeof(List<Root>));

Classes:

[DataContract]
public class Root 
{
    [DataMember]
    [JsonProperty("Meta Data")]
    public MetaData MetaData { get; set; }

    [DataMember]
    [JsonProperty("Time Series (Daily)")]
    public TimeSeriesDaily TimeSeriesDaily { get; set; }
}     

[DataContract]
public class MetaData
{
    [DataMember]
    [JsonProperty("1. Information")]
    public string _1Information { get; set; }

    [DataMember]
    [JsonProperty("2. Symbol")]
    public string _2Symbol { get; set; }

    [DataMember]
    [JsonProperty("3. Last Refreshed")]
    public string _3LastRefreshed { get; set; }

    [DataMember]
    [JsonProperty("4. Output Size")]
    public string _4OutputSize { get; set; }

    [DataMember]
    [JsonProperty("5. Time Zone")]
    public string _5TimeZone { get; set; }
}

[DataContract]
public class _20220203
{
    [DataMember]
    [JsonProperty("1. open")]
    public string _1Open { get; set; }

    [DataMember]
    [JsonProperty("2. high")]
    public string _2High { get; set; }

    [DataMember]
    [JsonProperty("3. low")]
    public string _3Low { get; set; }

    [DataMember]
    [JsonProperty("4. close")]
    public string _4Close { get; set; }

    [DataMember]
    [JsonProperty("5. volume")]
    public string _5Volume { get; set; }
}

public class _20220202
{
    [JsonProperty("1. open")]
    public string _1Open { get; set; }

    [JsonProperty("2. high")]
    public string _2High { get; set; }

    [JsonProperty("3. low")]
    public string _3Low { get; set; }

    [JsonProperty("4. close")]
    public string _4Close { get; set; }

    [JsonProperty("5. volume")]
    public string _5Volume { get; set; }
}

JSON:

{
"Meta Data": {
    "1. Information": "Daily Prices (open, high, low, close) and Volumes",
    "2. Symbol": "IBM",
    "3. Last Refreshed": "2022-02-04",
    "4. Output Size": "Compact",
    "5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
    "2022-02-04": {
        "1. open": "137.8600",
        "2. high": "138.8200",
        "3. low": "136.2150",
        "4. close": "137.1500",
        "5. volume": "4142045"
    },
    "2022-02-03": {
        "1. open": "137.0000",
        "2. high": "138.7600",
        "3. low": "135.8310",
        "4. close": "137.7800",
        "5. volume": "6100777"
    }
  }
}

I could not get above to work sourcing : "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=demo"

The objective is to return a List<Root> of strongly typed objects.

CodePudding user response:

You don't have an array value there. You should deserialize to object Root obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(response).

{
"Meta Data": {
    "1. Information": "Daily Prices (open, high, low, close) and Volumes",
    "2. Symbol": "IBM",
    "3. Last Refreshed": "2022-02-04",
    "4. Output Size": "Compact",
    "5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
    "2022-02-04": {
        "1. open": "137.8600",
        "2. high": "138.8200",
        "3. low": "136.2150",
        "4. close": "137.1500",
        "5. volume": "4142045"
    },
    "2022-02-03": {
        "1. open": "137.0000",
        "2. high": "138.7600",
        "3. low": "135.8310",
        "4. close": "137.7800",
        "5. volume": "6100777"
    }
  }
}

Try to parse Time Series (Daily) as Dictionary<string, object>

  {
"Meta Data": {
    "Information": "Daily Prices (open, high, low, close) and Volumes",
    "Symbol": "IBM",
    "Last Refreshed": "2022-02-04",
    "Output Size": "Compact",
    "Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
    "2022-02-04": {
        "open": "137.8600",
        "high": "138.8200",
        "low": "136.2150",
        "close": "137.1500",
        "volume": "4142045"
    },
    "2022-02-03": {
        "open": "137.0000",
        "high": "138.7600",
        "low": "135.8310",
        "close": "137.7800",
        "volume": "6100777"
    }
  }
}

If you want enumerate object then you can use JsonDocument:

        var jDoc = JsonDocument.Parse(content, new JsonDocumentOptions() { CommentHandling = JsonCommentHandling.Skip, AllowTrailingCommas = true });
        var root = jDoc.RootElement.EnumerateObject();

From the discussion in the comment, I believe it will help:

[DataContract]
public class Root 
{
    [DataMember]
    [JsonProperty("Meta Data")]
    public MetaData MetaData { get; set; }

    [DataMember]
    [JsonProperty("Time Series (Daily)")]
    public Dictionary<string, DataItem> TimeSeriesDaily { get; set; }
}     

[DataContract]
public class MetaData
{
    [DataMember]
    [JsonProperty("1. Information")]
    public string _1Information { get; set; }

    [DataMember]
    [JsonProperty("2. Symbol")]
    public string _2Symbol { get; set; }

    [DataMember]
    [JsonProperty("3. Last Refreshed")]
    public string _3LastRefreshed { get; set; }

    [DataMember]
    [JsonProperty("4. Output Size")]
    public string _4OutputSize { get; set; }

    [DataMember]
    [JsonProperty("5. Time Zone")]
    public string _5TimeZone { get; set; }
}

[DataContract]
public class DataItem
{
    [DataMember]
    [JsonProperty("1. open")]
    public string _1Open { get; set; }

    [DataMember]
    [JsonProperty("2. high")]
    public string _2High { get; set; }

    [DataMember]
    [JsonProperty("3. low")]
    public string _3Low { get; set; }

    [DataMember]
    [JsonProperty("4. close")]
    public string _4Close { get; set; }

    [DataMember]
    [JsonProperty("5. volume")]
    public string _5Volume { get; set; }
}

And then to parse your JSON and return the list you should do like this:

public List<Root> ParseJson(string json)
{
    Root obj = Newtonsoft.Json.JsonConvert.Deserialize<Root>(json);
    return new List<Root> { obj };
}

CodePudding user response:

try this

    string json = string.Empty;
    using (var client = new HttpClient())
    {
        var baseAddress = "https://www.alphavantage.co";
        var api = "/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=demo";

        client.BaseAddress = new Uri(baseAddress);
        var contentType = new MediaTypeWithQualityHeaderValue("application/json");
        client.DefaultRequestHeaders.Accept.Add(contentType);
        var response = client.GetAsync(api).Result;
        if (response.IsSuccessStatusCode)
            json = response.Content.ReadAsStringAsync().Result;
    }
    Data data =JsonConvert.DeserializeObject<Data>(json);

and you can get TimeSeriesDaily list

List<TimeSeriesDaily> timeSeriesDaily = data.TimeSeriesDaily.Select(tsd => AddDate(tsd) ).ToList();

private TimeSeriesDaily AddDate(KeyValuePair<string, TimeSeriesDaily> tsdd)
{
    var tsd=tsdd.Value;
    tsd.Date=tsdd.Key;
    return tsd;
}

classes

public partial class Data
    {
        [JsonProperty("Meta Data")]
        public MetaData MetaData { get; set; }

        [JsonProperty("Time Series (Daily)")]
        public Dictionary<string, TimeSeriesDaily> TimeSeriesDaily { get; set; }
    }

    public partial class MetaData
    {
        [JsonProperty("1. Information")]
        public string The1Information { get; set; }

        [JsonProperty("2. Symbol")]
        public string The2Symbol { get; set; }

        [JsonProperty("3. Last Refreshed")]
        public DateTimeOffset The3LastRefreshed { get; set; }

        [JsonProperty("4. Output Size")]
        public string The4OutputSize { get; set; }

        [JsonProperty("5. Time Zone")]
        public string The5TimeZone { get; set; }
    }

public partial class TimeSeriesDaily
{
    [JsonIgnore]
    public string Date { get; set; }
    
    [JsonProperty("1. open")]
    public string The1Open { get; set; }

    [JsonProperty("2. high")]
    public string The2High { get; set; }

    [JsonProperty("3. low")]
    public string The3Low { get; set; }

    [JsonProperty("4. close")]
    public string The4Close { get; set; }

    [JsonProperty("5. volume")]
    public long The5Volume { get; set; }
}
  •  Tags:  
  • Related