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

相關文章
相關標籤/搜索