当前位置:   article > 正文

C#中使用JsonConvert解析JSON

jsonconvert
using Newtonsoft.Json

首先添加Newtonsoft.Json的引用

1.JSON序列化

string JsonStr= JsonConvert.SerializeObject(Entity);

  1. public class RecordResult
  2. {
  3. [JsonProperty("status")]
  4. public int Status { get; set; }
  5. [JsonProperty("error")]
  6. public int Error { get; set; }
  7. }
  8. RecordResult result = new RecordResult();
  9. result.Status = 1;
  10. result.Error = "Error message";
  11. string jsonStr = JsonConvert.SerializeObject(result);

2.JSON反序列化
Class model = JsonConvert.DeserializeObject<Class>(jsonstr);

  1. var result = (RecordResult)JsonConvert.DeserializeObject(jsonstring, typeof(RecordResult))
  2. //或者
  3. var result = JsonConvert.DeserializeObject<RecordResult>(jsonstring)

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号