Tuesday, May 2, 2017

JSON to C# dataset

The following will deserialize a Canvas json response into a dataset

using System;
using System.Data;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Xml;

        private void temp2()
        {
            string pjson = System.IO.File.ReadAllText(@"c:\temp\json.txt");

            DataSet dsRecognize = new DataSet();
           
            XmlDocument xdRecognize = new XmlDocument();
            xdRecognize = (XmlDocument)JsonConvert.DeserializeXmlNode("{\"Row\":" + pjson + "}", "root");

            dsRecognize.ReadXml(new XmlNodeReader(xdRecognize));

            dataGridView1.DataSource = dsRecognize.Tables[0];

        }
**************************
Some notes:
c:\temp\json.txt is a copy of a json response. Remove the initial "while(1);" but keep the brackets "[]"

I sent the first table to a dataset, and it seemed to be good data.

No comments:

Post a Comment