-1

I am new to C# and I have a certain JSON format that I am trying to read. I have managed to bring in the whole string but I only need the "data" bit.

{
    "notes": {
        "status": "success",
        "datasource": "Internet",
        "result_rows": 35
    },
    "data": [
        ["Date", "Visits", "Pageviews", "Clicks", "Bounces", "Time on Website"],
        ["2018-10-01", 57, 145, 39, 16, 4112],
        ["2017-10-02", 32, 29, 34, 29, 89],
        ["2019-10-03", 40, 129, 18, 35, 122216],
        ["2016-12-04", 15, 12, 13, 10, 14408],
        ["2019-01-08", 13, 23, 110, 72, 12277],
        ["2011-12-06", 45, 44, 33, 5, 33784],
        ["2018-06-15", 30, 428, 454, 64, 67319],
        "columns": [{
                "name": "Date",
                "field": "date",
                "type": "dim",
                "data_type": "string.time.date"
            },
            {
                "name": "Visits",
                "field": "visits",
                "type": "met",
                "data_type": "int.number.value"
            },
            {
                "name": "Pageviews",
                "field": "pageviews",
                "type": "met",
                "data_type": "int.number.value"
            },
            {
                "name": "Clicks",
                "field": "clicks",
                "type": "met",
                "data_type": "int.number.value"
            },
            {
                "name": "Bounces",
                "field": "bounces",
                "type": "met",
                "data_type": "int.number.value"
            },
            {
                "name": "Time on Website",
                "field": "websiteduration",
                "type": "met",
                "data_type": "float.number.value"
            }
        ]
    }

My code fails at:

List<item> Data = JsonConvert.DeserializeObject<List<item>>(jsonString);

I am not too sure how to only extract the data bit in a proper format. Thank you in advance

The code that i am using currently

namespace API
{
    public class item
    {
        public string Date { get; set; }
        public string Visits { get; set; }
        public string Pageviews { get; set; }
        public string Clicks{get; set;}
        public string Bounces { get; set; }

        [JsonProperty("Time on website")]
        public string Time_on_Website { get; set; }
    }
}

namespace API
{
    public class Program
    {
        public static void Main(string[] args)
        {

            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("https://website.com"));

            WebReq.Method = "GET";

            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

            Console.WriteLine(WebResp.StatusCode);
            Console.WriteLine(WebResp.Server);

            string jsonString;
            using (Stream stream = WebResp.GetResponseStream())
            {
                 StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
                 jsonString = reader.ReadToEnd();
            }

            List<item> Data = JsonConvert.DeserializeObject<List<item>>(jsonString);

            Console.WriteLine(Data);


        }

    }
}
Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
escobar
  • 23
  • 7

1 Answers1

0

i think firstly you should add a class like this

public class Items
{
    public List<Item> data { get; set; }
}

and then

Items items = new JavaScriptSerializer().Deserialize<Items>(jsonString);

foreach (var item in items.data)
{
            ...
            ...
}

but i think your json string is in an invalid format.

ggeorge
  • 1,496
  • 2
  • 13
  • 19