c#string类型反序列化成字典类型
c# 实现string类型转化为字典类型:黄色底纹为需要引用的dll,可以在网站下载!
下载地址:http://download.csdn.net/download/xinping_168/4710720
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Json序列化
{
public class Product
{
public string productDetail { get; set; }
public Dictionary<string, string> ProductLineDetailList
{
get
{
if (string.IsNullOrWhiteSpace(productDetail))
{
return new Dictionary<string, string>();
}
try
{
var obj = JToken.Parse(productDetail);
}
catch (Exception ex)
{
throw new FormatException("ProductDetails不符合json格式.");
}
return JsonConvert.DeserializeObject<Dictionary<string, string>>(productDetail);
}
}
}
class Program
{
static void Main(string[] args)
{
var product = new Product();
product.productDetail = "{'size':'10','weight':'12'}";
foreach (var item in product.ProductLineDetailList)
{
Console.WriteLine(item.Key + " " + item.Value);
}
Console.Read();
}
}
}