C#中使用JsonConvert解析JSON


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)
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM