using Newtonsoft.JSON
首先添加 Newtonsoft.JSON 的引用
1.JSON 序列化
- string JsonStr= JsonConvert.SerializeObject(Entity);
- public class RecordResult
- {
- [JsonProperty("status")]
- public int Status {
- get; set;
- }
- [JsonProperty("error")]
- public int Error {
- get; set;
- }
- }
- RecordResult result = new RecordResult();
- result.Status = 1;
- result.Error = "Error message";
- string jsonStr = JsonConvert.SerializeObject(result);
2.JSON 反序列化
- Class model = JsonConvert.DeserializeObject<Class>(jsonstr);
- var result = (RecordResult)JsonConvert.DeserializeObject(jsonstring, typeof(RecordResult))
- // 或者
- var result = JsonConvert.DeserializeObject<RecordResult>(jsonstring)
来源: http://www.bubuko.com/infodetail-3109761.html