Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to serialize multiple DataTables to JSON which are interrelated and it is possible there will be additional table are all set in the mapping table. In this case, I have 4 DataTables.
	
	This is my first time using JSON, and I have found it really tricky to find a simple way of doing this. I have been using a StringBuilder at the moment to create a JSON string from the information in the dataset, but I have heard that it is possible to do this very simply with .NET 3.5 using the System.Runtime.Serialization namespace, though I have yet to find a simple article or blog on how this is done! What is the easiest way to do this?


What I have tried:

I want JSON in below format.

{quiz": {
        "Id": 2,
        "name": "C# and .Net Framework",
        "description": "C# and .Net Quiz (contains C#, .Net Framework, Linq, etc.)"
    },
    "config":{
        "shuffleQuestions": true,
        "showPager": false,
        "allowBack": true,
        "autoMove": true
    },
    "questions": [{
        "Id": 1010,
        "Name": "Which of the following assemblies can be stored in Global Assembly Cache?", 
        "QuestionTypeId": 1,
        "Options": [
            { "Id": 1055, "QuestionId": 1010, "Name": "Private Assemblies", "IsAnswer": false },
            { "Id": 1056, "QuestionId": 1010, "Name": "Friend Assemblies", "IsAnswer": false },
            { "Id": 1057, "QuestionId": 1010, "Name": "Public Assemblies", "IsAnswer": false },
            { "Id": 1058, "QuestionId": 1010, "Name": "Shared Assemblies", "IsAnswer": true }],
        "QuestionType": { "Id": 1, "Name": "Multiple Choice", "IsActive": true }
    },
    {
        "Id": 1011,
        "Name": "Which of the following .NET components can be used to remove unused references from the managed heap?", 
        "QuestionTypeId": 1,
        "Options": [
            { "Id": 1055, "QuestionId": 1010, "Name": "Language Infrastructure", "IsAnswer": false },
            { "Id": 1056, "QuestionId": 1010, "Name": "CLR", "IsAnswer": false },
            { "Id": 1057, "QuestionId": 1010, "Name": "Garbage Collector", "IsAnswer": true },
            { "Id": 1058, "QuestionId": 1010, "Name": "Class Loader", "IsAnswer": false },
            { "Id": 1058, "QuestionId": 1010, "Name": "CTS", "IsAnswer": false }],
        "QuestionType": { "Id": 1, "Name": "Multiple Choice", "IsActive": true }
    },
	    }]
}
Posted
Updated 25-May-18 5:49am
Comments
jaket-cp 25-May-18 12:30pm    
you can use JavaScriptSerializer to serialize a List<Dictionary<String, Object>>
https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx

1 solution

Have you tried json.net? It lets you do this:

string json = JsonConvert.SerializeObject(dataSet, Formatting.Indented);


Seriously, google is your friend.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900