Этот вопрос связан с форматом данных Json. У меня есть словарь с парой значений ключа. ConfigA 1200 ConfigB 1500 ConfigC 800 ConfigD 2 .I нужно преобразовать все словарные значения в 1 формате JSON [{ "ConfigA": "1200", "ConfigB": "1500", "ConfigC":» 800 "," ConfigD ":" 2 "}] Может ли кто-нибудь помочь мне, как это сделать?C# Dictionary to Json Format
-1
A
ответ
0
Добавить System.Web.Extensions.dll и попробуйте следующий код,
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
public class Program
{
public static void Main()
{
var dictionary = new Dictionary<string, int> {
{"ConfigA", 1200},
{"ConfigB", 1500},
{"ConfigC", 800},
{"ConfigD", 2}
};
var serializer = new JavaScriptSerializer();
Console.WriteLine(serializer.Serialize(dictionary));
}
}
0
Возможно так же, как this question
Просто из интереса, вы знаете, что вы создаете массив, содержащий один объект с 4 параметрами? Предполагая, что JavaScript станет объектом этой структуры, вы будете ссылаться на данные [0] .config *. Вы можете отказаться от квадратных скобок в начале и в конце, чтобы облегчить понимание кода JIS получателя. Пример до и после ниже.
// using array
var data = jQuery.parseJSON('[{"ConfigA":"1200","ConfigB":"1500","ConfigC":"800","ConfigD":"2"}]')
var A = data[0].configA
alert('Value of A=' + A) // Will show 1200
var B = data[0].configB
alert('Value of B=' + B) // Will show 1500
// without array
var dataV2 = jQuery.parseJSON('{"ConfigA":"1200","ConfigB":"1500","ConfigC":"800","ConfigD":"2"}')
var A2 = data.configA
alert('Value of A2=' + A2) // Will show 1200
var B2 = data.configB
alert('Value of B2=' + B2) // Will show 1500
Смежные вопросы
- 1. JSON to Dictionary C#
- 2. long-format pandas dataframe to dictionary
- 3. C# Dictionary to Dictionary AutoMapping
- 4. Json file to dictionary
- 5. JSON to Python Dictionary
- 6. Deserializing JSON to Dictionary
- 7. Python json dictionary to array
- 8. alamofire encodingCompletion JSON to dictionary
- 9. C# Dictionary to Kendo DataSource
- 10. flattening json to csv format
- 11. Laravel array to json format
- 12. NSDate to GMTDate to JSON format
- 13. C# Serialize Dictionary JSON
- 14. IronRuby Hash to C# Dictionary
- 15. JSON string to dictionary object error
- 16. C# json parsing format
- 17. C# format line to column
- 18. print json object to string format
- 19. Dump Python dictionary to JSON file
- 20. JSON Dictionary object to string в Swift
- 21. echo selected items to JSON format
- 22. C# JSON Deserialization Dictionary Exception
- 23. parse/convert cookie to JSON format
- 24. C# json format too complex
- 25. C# linq get count to dictionary
- 26. C++ to .net dictionary, получил MarshalDirectiveException
- 27. $ AddToSet to Dictionary (MongoDB, C#) дублирующие записи
- 28. JSON to C# объект
- 29. store dictionary to hashtable
- 30. ServiceStack - dictionary to csv
использование новыйtonsoft. http://www.newtonsoft.com/json/help/html/DeserializeDictionary.htm –
Возможный дубликат [Как преобразовать словарь в строку JSON в C#?] (http://stackoverflow.com/questions/5597349/как-делать-я-новообращенный-а-словарь к-а-JSon-строка-в-с) –