0

Json data:

{
"template": {
    "section": "Total",
    "totalRecievedTillDate": null,
    "receivedFiles": "0",
    "attendedFiles": "0",
    "pendingFiles": "0",
    "totalActionPendingTillDate": null,
    "totalActionCompletedTappal": null,
    "last15DaysActionCompleted": null,
    "last15DaysRecievedTappal": null
},
"sectiontappals": [
    {
        "section": "Collectorate-A Section",
        "totalRecievedTillDate": null,
        "receivedFiles": "0",
        "attendedFiles": "0",
        "pendingFiles": "0",
        "totalActionPendingTillDate": null,
        "totalActionCompletedTappal": null,
        "last15DaysActionCompleted": null,
        "last15DaysRecievedTappal": null
    },
    {
        "section": "Collectorate-B Section",
        "totalRecievedTillDate": null,
        "receivedFiles": "0",
        "attendedFiles": "0",
        "pendingFiles": "0",
        "totalActionPendingTillDate": null,
        "totalActionCompletedTappal": null,
        "last15DaysActionCompleted": null,
        "last15DaysRecievedTappal": null

I want to delete first filed (template) and want to bind data from sectiontappals to datagridview in C#. Is it possible?

I tried this:

using (var client = new HttpClient()) {
    string path = "uri";
    using (var response = await client.GetAsync(path)) {
        if (response.IsSuccessStatusCode) {
            var data = await response.Content.ReadAsStringAsync();
            dashbordgrd.DataSource = JsonConvert.DeserializeObject<List<listclass>>(data);
        }
    }
}
Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34

1 Answers1

0

Create below class, Just for the sake of example I've created only one property in template class.

public class template
    {
        public string section{get; set;}
        private List<sectiontappal> _sectionappal;
        public List<sectiontappal> sectiontappals
        {
            get
            {
                if (_sectionappal == null)
                    _sectionappal = new List<sectiontappal>();
                return _sectionappal;
            }
            set
            {
                _sectionappal = value;
            }
        }
    }

Add NewtonJson to your project & Use below code to achieve your ask:

System.IO.StreamReader reader = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + "/1.txt");
string result = reader.ReadToEnd();
var data = JsonConvert.DeserializeObject<template>(result);
dataGridView1.DataSource = data.sectiontappals;

I've placed your json in file with name 1.txt.

Thanks

Brijesh
  • 369
  • 2
  • 10
  • actually the Json data is from URL. can i place url in place of ""/1.txt" ? – Narasingh Rao pediredla Feb 13 '17 at 04:55
  • Use WebClient class, it is available in System.Net namespace. using (WebClient wc = new WebClient()) { var json = wc.DownloadString("url"); //Parse the result } Use this link: http://stackoverflow.com/questions/5566942/how-to-get-a-json-string-from-url – Brijesh Feb 13 '17 at 05:10
  • :) I think you wanted to thank me! Nevertheless I'm happy it worked for you – Brijesh Feb 13 '17 at 12:14