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)

猜你喜欢

转载自blog.csdn.net/watson2017/article/details/81949829
今日推荐