Chandan KIIT32 Ответов: 1

Как преобразовать набор данных с несколькими датируемый в 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?


Что я уже пробовал:

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

jaket-cp

вы можете использовать класс javascriptserializer, чтобы сериализовать список<словарь на<строка, объект&ГТ;&ГТ;
https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs. 110).aspx

1 Ответов

Рейтинг:
2

#realJSOP

А вы пробовали json.net-что? Это позволяет вам сделать это:

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


Серьезно, google-твой друг.