C# - Trying to convert Dictionary to JSON
Use Newtonsoft.Json
to get the right json. Given you have it referenced on your project use the namespace:
using Newtonsoft.Json;
And use the JsonConvert.SerializeObject
static method:
string json = JsonConvert.SerializeObject(SendData);
converting dictionary string string to json string
Try this using extension methods.
public static class Extensions
{
public static string FromDictionaryToJson(this Dictionary<string, string> dictionary)
{
var kvs = dictionary.Select(kvp => string.Format("\"{0}\":\"{1}\"", kvp.Key, string.Concat(",", kvp.Value)));
return string.Concat("{", string.Join(",", kvs), "}");
}
public static Dictionary<string, string> FromJsonToDictionary(this string json)
{
string[] keyValueArray = json.Replace("{", string.Empty).Replace("}", string.Empty).Replace("\"", string.Empty).Split(',');
return keyValueArray.ToDictionary(item => item.Split(':')[0], item => item.Split(':')[1]);
}
}
Here is how you would use them after.
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> dictss = new Dictionary<string, string>();
dictss.Add("onekey", "oneval");
dictss.Add("twokey", "twoval");
dictss.Add("threekey", "threeval");
dictss.Add("fourkey", "fourval");
dictss.Add("fivekey", "fiveval");
string jsonString = dictss.FromDictionaryToJson(); //call extension method
Console.WriteLine(jsonString);
Dictionary<string, string> dictss2 = jsonString.FromJsonToDictionary(); //call extension method
foreach(KeyValuePair<string,string> kv in dictss2)
Console.WriteLine(string.Format("key={0},value={1}", kv.Key, kv.Value));
}
}
Or using plain functions
public string FromDictionaryToJson(Dictionary<string, string> dictionary)
{
var kvs = dictionary.Select(kvp => string.Format("\"{0}\":\"{1}\"", kvp.Key, string.Join(",", kvp.Value)));
return string.Concat("{", string.Join(",", kvs), "}");
}
public Dictionary<string, string> FromJsonToDictionary(string json)
{
string[] keyValueArray = json.Replace("{", string.Empty).Replace("}", string.Empty).Replace("\"", string.Empty).Split(',');
return keyValueArray.ToDictionary(item => item.Split(':')[0], item => item.Split(':')[1]);
}
How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?
Json.NET does this...
string json = @"{""key1"":""value1"",""key2"":""value2""}";
var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
More examples: Serializing Collections with Json.NET
Related Topics
How to Get Number of Rows Using Sqldatareader in C#
How to Create a Dynamic Email Template That Can Be Modified Without Changing Code in C# .Net MVC
How to Check Byte Array Empty or Not
How to Generate a System (Pc/Laptop) Hardware Unique Id in C#
Restsharp Serializing Json Objects to Post Parameters
Asp.Net Core Form Post Results in a Http 415 Unsupported Media Type Response
Getting the Latest File Modified from Azure Blob
How to Perform Join Between Multiple Tables in Linq Lambda
How to Correctly Write Parallel.For With Async Methods
How to Remove Windows Credentials
Passing Datetimeoffset as Webapi Query String
Reference External Dll in .Net Core Project
Asp.Net Core, Change Default Redirect for Unauthorized
Kill Child Process When Parent Process Is Killed
Setting Connection String With Username and Password in Asp.Core MVC
Replace Single Backslash With Double Backslash
String Array to Int Array Conversion
Linq to SQL Left Outer Join Using Lambda Syntax and Joining on 2 Columns (Composite Join Key)