c#string类型反序列化成字典类型

c# 实现string类型转化为字典类型:黄色底纹为须要引用的dll,能够在网站下载!json

下载地址:http://download.csdn.net/download/xinping_168/4710720c#

 

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格式.");
}spa

return JsonConvert.DeserializeObject<Dictionary<string, string>>(productDetail);
}
}
}
class Program
{
static void Main(string[] args)
{
var product = new Product();
product.productDetail = "{'size':'10','weight':'12'}";.net


foreach (var item in product.ProductLineDetailList)
{
Console.WriteLine(item.Key + " " + item.Value);
}orm

Console.Read();
}
}
}get

相关文章
相关标签/搜索