如何将具有多个数据的数据集转换为json? [英] How to convert dataset with multiple datable to json?

查看:105
本文介绍了如何将具有多个数据的数据集转换为json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 }
    },
	    }]
}

推荐答案

你试过json.net吗?它允许你这样做:



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

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





说真的,谷歌是你的朋友。



Seriously, google is your friend.


这篇关于如何将具有多个数据的数据集转换为json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆