此問題與Json數據格式有關。我有一個帶有Key值對的字典。 ConfigA 1200 ConfigB 1500 ConfigC 800 ConfigD 2 .I需要將所有字典值轉換爲1種JSON格式 [{ 「ConfigA」: 「1200」, 「ConfigB」: 「1500」, 「ConfigC」:」 800「,」ConfigD「:」2「}] 任何人都可以幫助我如何做到這一點?C#Dictionary to Json格式
-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將成爲此結構的目標,那麼您將引用data [0] .config *。您可能想要在開始和結束時忽略方括號以使接收方JS代碼更易於理解。下面之前和之後的示例。
// 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. C#List <dictionary> To Json
- 2. alamofire encodingCompletion JSON to dictionary
- 3. Dictionary to Json object,「Expected expression」
- 4. Dictionary to CSV - Python
- 5. Dictionary to CSV
- 6. Python AST to dictionary structure
- 7. Swift 1D Dictionary to 2D
- 8. Json反序列化形式Dictionary <string,Dictionary <string,string >>
- 9. Hashtable to Dictionary <> syncroot。
- 10. vb.net linq to dictionary rounding error
- 11. Swift 3 NSDictionary to Dictionary conversion causes NSInvalidArgumentException
- 12. Sorting HTML table or Dictionary keys to make specific layout
- 13. 在Dictionary中創建格式化值python
- 14. Transforming Dictionary <T,U> to Dictionary <T,U.PropertyValue>
- 15. C#使慾望JSON格式
- 16. c#json解析格式
- 17. c#json格式太複雜
- 18. JSON to object using C#JavaScriptSerializer
- 19. Dynamic vs Dictionary [C#]
- 20. C#Ordered dictionary index
- 21. Dictionary to Custom KeyValuePair list - 無法轉換(C#.Net 4.0)
- 22. C#JSON反序列化Dictionary <string,Dictionary <string,string >>可選?
- 23. 字典JSON格式
- 24. Transfrom ICollection。到Dictionary()c#
- 25. C#中的「Dictionary nest」?
- 26. C#Stream Dictionary問題
- 27. Speech to Text音頻格式
- 28. String to UK日期格式
- 29. Crystal Report to Excel格式
- 30. 格式化JSON
使用newtonsoft。 http://www.newtonsoft.com/json/help/html/DeserializeDictionary.htm –
可能的重複[如何將字典轉換爲C#中的JSON字符串?](http://stackoverflow.com/questions/5597349 /怎麼辦,我變頻-A-字典至A-JSON字符串,在-C) –