使用 ExpandoObject 和 IDictionary 从动态 JSON 中删除转义字符''



我正在使用ExpandoObject和IDictionary创建一个动态JSON。

我的示例代码如下:

DataTable dt_MappedColumns = new DataTable();
dt_MappedColumns.Columns.Add("Sr.No");
dt_MappedColumns.Columns.Add("TColumnName");
dt_MappedColumns.Columns.Add("AColumnName");
dt_MappedColumns.Rows.Add("1", "Apple", "Lion");
dt_MappedColumns.Rows.Add("2", "Orange", "Tiger");
dt_MappedColumns.Rows.Add("3", "Mango", "Fox");
dt_MappedColumns.Rows.Add("4", "Orange", "Wolf");
var dictionary1 = new Dictionary<string, object>();
foreach (DataRow dr in dt_MappedColumns.Rows)
{
string key = dr["TColumnName"].ToString();
string value = dr["AColumnName"].ToString();
if (!dictionary1.ContainsKey(key))
{
// key does not already exist, so add it
dictionary1.Add(key, value);
}
else
{
// key exists, get the existing value
object existingValue = dictionary1[key];
if (existingValue is string)
{
// replace the existing string value with a list
dictionary1[key] = new List<string> { (string)existingValue, value }; 
}
else
{
// the existing value is a list, so just add the new value to it
((List<string>)existingValue).Add(value);
}
}
}
string Manjson = JsonConvert.SerializeObject(dictionary1);

如此Obtanined的JSON字符串如下,带有转义符{\}:

"{\"苹果\":\"狮子\",\"橙色\":[\"老虎\","狼\"],\"芒果\":"狐狸\"}"。

我希望删除转义符,所需的JSON如下所示:

"{"苹果:"狮子","橙":["老虎","狼"],"芒果":"狐狸"}">

***编辑***

有趣的是,如果我在控制台中打印JSON,它没有转义符,但当我插入Oracle DB表时,它插入了转义符。

将Visual Studio 2010 Professional与Oracle 11g和PL/SQL 11.2 一起使用

解决方案是什么:

感谢您的帮助:(

我通过将其转换为JObject并按如下方式使用它来解决问题:

使用Newtonsoft.Json.Linq;

var Varjson=JObject.Parse(Manjson(;

其中Manjson是我的带有转义符('/'(的JSON字符串。

Object Varjson将包含不带转义符的反序列化Json,如下所示:

"{quot;苹果":"狮子"、"橘子":[quot;老虎"、"狼"]、"芒果":"狐狸";

:(

最新更新