Create complex json from datatable c#

I have 3 datatables in my dataset. Table A has one to many relationship with B and C. I want to create Json like below using Linq in c#. Can someone please help me? I would like to thanks in advance to everyone who will guide me or provide me input to my question.

{ "A": [ { "id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55, "B": [ { "id": "1001", "type": "Regular" } ], "C": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" } ] } ], "A": [ { "id": "0002", "type": "Cupcake", "name": "Cake", "ppu": 2.43, "B": [ { "id": "1001", "type": "Regular" } ], "C": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" } ] } ] }

-------------Problems Reply------------

First of all, you have to create these classes,

public class Result
{
public List<TableA> A { get; set; }
}
public class TableA
{
public TableA()
{
B = new List<TableB>();
C = new List<TableC>();
}
public string id { get; set; }
public string type { get; set; }
public string name { get; set; }
public float ppu { get; set; }
public virtual List<TableB> B { get; set; }
public virtual List<TableC> C { get; set; }
}

public class TableB
{
public string id { get; set; }
public string type { get; set; }
}
public class TableC
{
public string id { get; set; }
public string type { get; set; }
}

After then, store the values in result object of Result class and serialize this using NewtonSoft serializer as mentioned below:

Result result;
JsonConvert.SerializeObject(result);

You can make it simple by without creating any classes and utilizing "dynamic" type. C# 4 allows you to create your types dynamically and use them like any declared types or classes.

For more on dynamic type: http://www.codeproject.com/Articles/69407/The-Dynamic-Keyword-in-C

First thing you need is to create dynamic type and fill its properties like the Json you require. Please see this post to see how the structure of Json and dynamic correlates: http://www.codeproject.com/Tips/834005/storing-GeoJson-coordinates.

There are many ways to make your iteration on DataSet to be dynamically adjust based on your data and columns. You may take further help from: how to convert datatable to json in C#.

I hope this might help.

Category:javascript Views:1 Time:2018-09-13

Related post

  • Outputting complex JSON from a MySQL result with PHP 2011-11-28

    I have just completed a Sencha Touch tutorial which is working fine. I'm currently in the process of converting static JSON data file (which the tutorial utilises) into a dynamically generated data (JSON) file from a MySQL database with PHP. The JSON

  • JSON and Jquery: Create nested JSON from nested list elements 2011-10-28

    I have created this jsfiddle: http://jsfiddle.net/mfnxm/1/ I am trying to create this JSON: [{ "link":"/about", "title":"About", "nodes":[{ "link":"/staff", "title":"Staff", "nodes":[{ "link":"/heads", "title":"Dept Heads" },{ "link":"/support", "tit

  • Create nested JSON from flat csv 2011-10-30

    Trying to create a 4 deep nested JSON from a csv based upon this example: Region,Company,Department,Expense,Cost Gondwanaland,Bobs Bits,Operations,nuts,332 Gondwanaland,Bobs Bits,Operations,bolts,254 Gondwanaland,Maureens Melons,Operations,nuts,123 A

  • How to create an Ilist from DataTable with unknown columns 2010-10-27

    I have a DataTable that was built from dynamically created SQL, so I do not know the number of columns in the datatable. How can I convert this datatable into an IList? EDIT: I am then going to use this to send to a Telerik Grid on the page. --------

  • Creating database schema from DataTable 2011-07-11

    Is it possible, without writing any SQL or using any 3rd party libraries to create a database schema from several ADO.NET DataTables? --------------Solutions------------- Not sure how "not sql" this is, but you can use SQL Server Management Objects.

  • How to create complex Json response in ruby in rails 2011-06-14

    I am working on ruby on rails project and I want to add respond to Json. One simple way is:-- def index @users = User.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @users } format.json { render :json => @us

  • Creating complex model from a basic table 2013-03-06

    I'm not really sure how to phrase this question. Basically I have a table in my database like so: Table1 _________ |id | |_________| |services | |_________| |solutions| |_________| but in my model I'm trying to map it to three objects: class studies

  • Filter Json from ASP.NET MVC JsonResult from a complex object 2010-11-01

    I am trying to post from a complex object from data model, a Json, unfortunately, I don’t want to pass all the hierarchy (person->HasMany Orders/ Orders HasMany Products etc.) but only the “first level” (for example person names for a grid view).

  • How can I create a JSON structure dynamically from Go? 2009-11-15

    As in, serializing JSON. My current code doesn't work, and I think it must have something to do with the fact that _Map, _String, etc. are not public. // vim:ft=go:ts=2 package main import "json" import "fmt" import vector "container/vector" func mai

  • Creating a json obj from a string when working without a net connection? 2010-05-27

    I have a json object returned from a third party api, it looks like: {"version":"1.0","encoding":"UTF-8"} I'm going to be working on my project without a network connection, so I have to do everything locally. How can I create an instance of a json o

  • Creating JSON from a comma delimited string with PHP 2010-09-10

    I am trying to create JSON from a comma delimited string, the string look something like this: One,Two,Three,Four,Five,Six,Seven I need it to look something like this: [{"test":"One"},{"test":"Two"},{"test":"Three"},{"test":"Four"},{"test":"Five"},{"

  • How to create a complex type from a stored procedure that uses exec()? 2010-09-16

    I want to create a complex type to use within an entity manager from a query constructed dynamically and executed with exec(). Is it possible?; since I'm writing a filter, what would you do instead if it is not possible? Also, I'm evaluating using li

  • ASP.NET MVC / C# - How to Convert/Create Excel (xls) with formatting from DataTable/List in MVC? 2011-03-25

    I was able to create Excel xls file from DataTable following this link. But how do I format the contents of the XLS file? Thanks. --------------Solutions------------- I have personally used NPOI with good results. Using this technique you cannot form

  • Create multidimentional JSon array from multiple tables 2011-04-02

    I am trying to create a json array with a structure like this: var cars = [ {name: 'Honda', models: [ {name: 'Accord', features: ['2dr', '4dr']}, {name: 'CRV', features: ['2dr', 'Hatchback']}, {name: 'Pilot', features: ['base', 'superDuper']} ]}, {na

  • iPhone: How to programmatically create a json file and insert the data obtained from my Sqlite database into it? 2011-05-21

    I want to get data from my Sqlite database and put it into this JSON file. How can I create a json file and insert the data obtained from my database into the JSON file? I want data in JSON file in this format: { "data": [ { "data": [ null, null, 30,

  • How to dynamically create a JSON object in c# (from an ASP.NET resource file)? 2011-07-07

    I need to serialize the strings from a resource file (.resx) into a JSON object. The resource file's keys are in flux and thus I cannot just create a C# object that accepts the appropriate values. It needs to be a dynamic solution. I am able to loop

  • How to create Backbone Collection from a JSON API call that returns root parameters as well as array 2011-08-13

    Backbone.js noob here. I want to create a collection, from a JSON API external to my application. Specifically, the api from Stackoverflow. I know I should set the url parameter from a collection like this: App.Collections.Users = Backbone.Collection

  • Looking for lib to create whole form from JSON? 2011-08-25

    I'm looking for a JS lib to build a whole form from JSON, inputEx looked promising at first but afaict I can only create individual elements from JSON with this lib, and I want something to make a whole form. So inputEx is close, and I could probably

  • Creating a HashSet from a column of DataTable 2011-11-15

    Is there a built in way (LINQ maybe?) of taking all the row values from a specific column from a DataTable and creating a HashSet from it? The column is also of type String. I could obviously do this in a loop but I was wondering if there is another

  • Create custom JSON string from form 2012-01-31

    I have a form I'd like to submit as JSON via jQuery AJAX, so that I can read it into a temporary table on the server-side using one my programming language's native methods (OpenEdge using READ-JSON). An example of the JSON my form needs to produce i

  • How to create a complex JSON using HashMap in android? 2012-02-09

    I am facing a problem creating a Json which is actually a complex one and i need to create it through HashMap only..I was actually looking for some recursive function that could be a best solution to my problem. JSON i need to create looks like.. {"p

  • How to iterate over a list and create a json array from it? 2012-02-20

    How can i iterate over a list and create a Json array to send back from my controller? Pseudo code is welcome! --------------Solutions------------- public ActionResult Foo() { var list = new List<string>(); list.Add("foo"); list.Add("bar"); ret

  • Creating a JSON result from SQL server database 2012-02-23

    I have an SQL server with the following layout Table ( id int title varchar(40), start Date(), end Date(), allDay bool, username varchar(40) ); I have gotten the following code from this blog to create a JSON object from the data I wish to use, howev

  • GSON create one object from complex object 2012-03-02

    Imagine that we have the following 2 classes: public class Test { private String element1; private SubTest subTest; } public class SubTest { private String element2; } If I create json from Test class I will have next string: {element1:null,subTest:{

  • Using the DotNet.HighCharts Library To Create Chart Based on DataTable Result From Webservice 2012-04-05

    We are trying to move away from Silverlight based chart controls to more plugin-less controls. We like HighCharts and are now testing implementing it into our existing codebase. We have found DotNet.HighCharts as a possible contender to quickly creat

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.137 (s). 11 q(s)