Asp.net C# 使用Newtonsoft.Json 进行通信

 
   
Asp.net C# 使用Newtonsoft.Json 进行通信
2010/03/20 11:26

1.将DataTable序列化成JSON

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

string Json = JsonConvert.SerializeObject(GetData(filename),new DataTableConverter()); //将datatable转成Json格式返回

2.反序列化JSON

 
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
//using System.Net.Json;
JObject o = JObject.Parse(@"{
  ""Stores"": [
     ""Lambton Quay"",
     ""Willis Street""
  ],
  ""Manufacturers"": [
     {
       ""Name"": ""Acme Co"",
       ""Products"": [
         {
           ""Name"": ""Anvil"",
           ""Price"": 50
         }
       ]
     },
     {
       ""Name"": ""Contoso"",
       ""Products"": [
         {
           ""Name"": ""Elbow Grease"",
           ""Price"": 99.95
         },
         {
           ""Name"": ""Headlight Fluid"",
           ""Price"": 4
         }
       ]
     }
  ]
}");
 
string name = (string)o.SelectToken("Manufacturers[0].Name");
// Acme Co
 
decimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price");
// 50
 
string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
// Elbow Grease

你可能感兴趣的:(Asp.net C# 使用Newtonsoft.Json 进行通信)