本篇文章为大家展示了 Newtonsoft.Json怎么在c#项目中使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
我们以如下的 Person 类举例,其中包含了常用的数据类型:
public class Person { public int ID { get; set; } public string Name { get; set; } public DateTime Birthday { get; set; } public bool IsVIP { get; set; } public float Account { get; set; } public string[] Favorites { get; set; } public string Remark { get; set; } }
创建一个 Person
实例:
Person person = new Person { ID = 1, Name = "张三", Birthday = DateTime.Parse("2000-01-02"), IsVIP = true, Account = 12.34f, Favorites = new string[] { "吃饭", "睡觉" } };
返回不缩进的 Json 字符串
JsonConvert.SerializeObject(person); {"ID":1,"Name":"张三","Birthday":"2000-01-02T00:00:00","IsVIP":true,"Account":12.34,"Favorites":["吃饭","睡觉"],"Remark":null}
返回缩进的 Json 字符串
JsonConvert.SerializeObject(person, Formatting.Indented); { "ID": 1, "Name": "张三", "Birthday": "2000-01-02T00:00:00", "IsVIP": true, "Account": 12.34, "Favorites": [ "吃饭", "睡觉" ], "Remark": null }
private string JsonIndentation(string str) { //string str = JsonConvert.SerializeObject(entity); JsonSerializer serializer = new JsonSerializer(); TextReader tr = new StringReader(str); JsonTextReader jtr = new JsonTextReader(tr); object obj = serializer.Deserialize(jtr); if (obj != null) { StringWriter textWriter = new StringWriter(); JsonTextWriter jsonWriter = new JsonTextWriter(textWriter) { Formatting = Formatting.Indented, Indentation = 4, IndentChar = ' ' }; serializer.Serialize(jsonWriter, obj); return textWriter.ToString(); } else { return str; } }
或者:
private string JsonIndentation(string json) { JObject obj = JObject.Parse(json); return obj.ToString(); }
JsonSerializerSettings settings = new JsonSerializerSettings(); // 设置日期格式 settings.DateFormatString = "yyyy-MM-dd"; // 忽略空值 settings.NullValueHandling = NullValueHandling.Ignore; // 缩进 settings.Formatting = Formatting.Indented; JsonConvert.SerializeObject(person, settings);
返回:
{ "ID": 1, "Name": "张三", "Birthday": "2000-01-02", "IsVIP": true, "Account": 12.34, "Favorites": [ "吃饭", "睡觉" ] }
JsonConvert.DeserializeObject<Person>(json);
JObject obj = new JObject(); obj.Add("ID", 1); obj.Add("Name", "张三"); obj.Add("Birthday", DateTime.Parse("2000-01-02")); obj.Add("IsVIP", true); obj.Add("Account", 12.34f); // 创建数组 JArray array = new JArray(); array.Add(new JValue("吃饭")); array.Add(new JValue("睡觉")); obj.Add("Favorites", array); obj.Add("Remark", null);
上例中的代码可以简化为:
JArray array = new JArray("吃饭", "睡觉");
string json = "{\"ID\":1,\"Name\":\"张三\",\"Birthday\":\"2000-01-02T00:00:00\",\"IsVIP\":true,\"Account\":12.34,\"Favorites\":[\"吃饭\",\"睡觉\"],\"Remark\":null}"; JObject obj = JObject.Parse(json);
JObject obj = JObject.FromObject(person);
用匿名对象创建 JObject
JObject obj = JObject.FromObject(new { name = "jack", age = 18 }); //显示 { "name": "jack", "age": 18 }
用初始化器
JObject obj = new JObject() { { "name", "jack" }, { "age", 18 } };
int id; if (obj["ID"] != null) id = obj["ID"].Value<int>();
Newtonsoft.Json.Linq 不支持直接获取数组,但是可以获取 List,然后再转化为数组。
string[] favorites; if (obj["Favorites"] != null) favorites = obj["Favorites"].Value<List<string>>().ToArray();
上述内容就是 Newtonsoft.Json怎么在c#项目中使用,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。