〇、前言
在日常开发中,对于 Json 的使用还是比较频繁的,特别是 Json 对象和字符串或者实体对象之间的转换。
虽然几乎天天用,但是总是感觉没那么明了,今天结合微软的 Newtonsoft.Json.Linq 类,试着详解一下,把相关的内容列一下。
一、Newtonsoft.Json.Linq 的层级结构
简单画个图,肯定比语言描述更清晰:

下面是层级结构中各个类的关系,以及都实现了哪些接口,通过查看他们实现的接口,就可知它的功能属性等:
| |
|
| |
public abstract class JToken : IJEnumerable<JToken>, |
| |
IEnumerable<JToken>, IEnumerable, IJsonLineInfo, ICloneable, |
| |
IDynamicMetaObjectProvider { } |
| |
|
| |
public abstract class JContainer : JToken, |
| |
IList<JToken>, ICollection<JToken>, IEnumerable<JToken>, |
| |
IEnumerable, ITypedList, IBindingList, IList, ICollection, |
| |
INotifyCollectionChanged { } |
| |
|
| |
public class JArray : JContainer, IList<JToken>, |
| |
ICollection<JToken>, IEnumerable<JToken>, IEnumerable { } |
| |
public class JConstructor : JContainer { } |
| |
public class JObject : JContainer, IDictionary<string, JToken>, |
| |
ICollection<KeyValuePair<string, JToken>>, IEnumerable<KeyValuePair<string, JToken>>, |
| |
IEnumerable, INotifyPropertyChanged, ICustomTypeDescriptor, INotifyPropertyChanging { } |
| |
public class JProperty : JContainer { } |
| |
|
| |
public class JValue : JToken, IEquatable<JValue>, |
| |
IFormattable, IComparable, IComparable<JValue>, IConvertible { } |
| |
|
| |
public class JRaw : JValue { } |
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
详情可参考:https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JToken.htm
三、各个类型的数据格式和简单用法
| JToken |
| |
|
| |
string jtokenstring1 = "\"Hello World\""; |
| |
string jtokenstring2 = "{\"name\":\"John\",\"age\":30}"; |
| |
JToken jtoken1 = JToken.Parse(jtokenstring1); |
| |
JToken jtoken2 = (JToken)JsonConvert.DeserializeObject(jtokenstring2); |
|
| JObect |
| |
|
| |
string jobjstring2 = "{\"name\":\"John\",\"age\":30}"; |
| |
JObject jobj1 = JObject.Parse(jtokenstring2); |
| |
JObject jobj2 = (JObject)JsonConvert.DeserializeObject(jtokenstring2); |
|
| JArray |
| |
|
| |
|
| |
string jarraystring1 = "[\"value1\",\"value2\"]"; |
| |
string jarraystring2 = "[{\"name\":\"John\",\"age\":30},{\"name\":\"Bob\",\"age\":20}]"; |
| |
JArray jarray1 = JArray.Parse(jarraystring2); |
| |
JArray jarray2 = (JArray)JsonConvert.DeserializeObject(jarraystring2); |
|
| JProperty |
| |
|
| |
string propertyname = "name"; |
| |
string propertyvalue = "John"; |
| |
JProperty jproperty = new JProperty(propertyname, propertyvalue); |
| |
var jp = jproperty.ToString(); |
| |
|
| |
|
| |
string jpropertystring1 = "\"name\": \"John\""; |
| |
|
| |
var jProperty1 = JProperty.Parse(jpropertystring1); |
| |
var jProperty2 = (JProperty)JsonConvert.DeserializeObject(jpropertystring1); |
| |
|
| |
|
| |
JObject jobject = new JObject(); |
| |
jobject.Add(jproperty); |
| |
string jsonstring = jobject.ToString(); |
| |
Console.WriteLine(jsonstring); |
| |
|
|
| JValue |
| |
JObject jsonObject = JObject.Parse("{\"a\":10,\"b\":\"Hello World\",\"c\":10}"); |
| |
|
| |
JValue jvaluea = (JValue)jsonObject["a"]; |
| |
JValue jvalueb = (JValue)jsonObject["b"]; |
| |
JValue jvaluec = (JValue)jsonObject["c"]; |
| |
|
| |
Console.WriteLine("jvaluea.Equals(jvalueb): " + jvaluea.Equals(jvalueb)); |
| |
Console.WriteLine("jvaluea.Equals(jvaluec): " + jvaluea.Equals(jvaluec)); |
| |
Console.WriteLine("jvaluea==jvaluec: " + (jvaluea == jvaluec)); |
| |
Console.WriteLine("jvalueb: " + jvalueb); |
| |
int intValue = jvaluea.Value<int>(); |
| |
Console.WriteLine("Integer value: " + intValue); |

|
四、相关类型转换
4.1 Json 对象转字符串
| |
|
| |
JObject jsonobject = JObject.Parse("{\"a\":10,\"b\":\"Hello World\",\"c\":10}"); |
| |
string jsonobjectstr = ObjectToJsonstr(jsonobject); |
| |
Console.WriteLine("jsonobjectstr:" + jsonobjectstr); |
| |
|
| |
JValue jvalue = (JValue)jsonobject["b"]; |
| |
Console.WriteLine("jvalue:" + jvalue); |
| |
string jvaluestr = ObjectToJsonstr(jvalue); |
| |
Console.WriteLine("jvaluestr:" + jvaluestr); |
| |
|
| |
JArray jsonarray = JArray.Parse("[{\"a\":10,\"b\":\"Hello World\",\"c\":10}]"); |
| |
string jsonarraystr = ObjectToJsonstr(jsonarray); |
| |
Console.WriteLine("jsonarraystr:" + jsonarraystr); |
| |
|
| |
|
| |
public static string ObjectToJsonstr(this object obj) |
| |
{ |
| |
if (obj == null) |
| |
return null; |
| |
return JsonConvert.SerializeObject(obj); |
| |
} |

4.2 json 字符串转 Json 对象
| |
|
| |
JObject jo = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(strjson); |
| |
JToken jo = (JToken)Newtonsoft.Json.JsonConvert.DeserializeObject(strjson); |
| |
JArray jo = (JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(strjson); |
4.3 json 字符串转实体对象 T
| |
|
| |
string jsonobject = "{\"name\":\"张三\",\"code\":\"1001\"}"; |
| |
var person = JsonstrToObject<Person>(jsonobject); |
| |
|
| |
public static T JsonstrToObject<T>(this string input) |
| |
{ |
| |
try |
| |
{ |
| |
return JsonConvert.DeserializeObject<T>(input); |
| |
} |
| |
catch (Exception ex) |
| |
{ |
| |
return default(T); |
| |
} |
| |
} |
4.4 实体对象转 Json 对象
| |
Person person = new Person() { name = "张三", code = "1001" }; |
| |
var personobj = Json_Object.TObjectToJsonobj<Person>(person); |
| |
string name = personobj["name"].ToString(); |
| |
|
| |
class Person |
| |
{ |
| |
public string name { get; set; } |
| |
public string code { get; set; } |
| |
} |
| |
|
| |
public static JObject TObjectToJsonobj<T>(T data) |
| |
{ |
| |
try |
| |
{ |
| |
JToken jtoken = JToken.FromObject(data); |
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
return jobject; |
| |
} |
| |
catch |
| |
{ |
| |
return null; |
| |
} |
| |
} |