About me
My blog | welcome the attention
The introduction
JSON is a data transfer format that we often use in our work, and parsing JSON is a problem we often face in the beginning process. NewtonSoftJSON is a set of JSON processing wrapper classes in C # that help you process JSON efficiently and easily.
NetonsoftJSON provides complete Documentation for use at the following address: Json.NET Documentation
Different usage scenarios for NetonsoftJSON
The common methods are serialization and deserialization
These are probably the two methods that we use most often
JsonConvert. DeserializeObject < T > (string json) / / deserialization JsonConvert. SerializeObject (object obj) / / serialization
Advanced features
See the article newtonsof. Json for advanced usage
Some fields do not want to be deserialized
Use the JSONIgnore attribute tag
public class Person { public int Age { get; set; } public string Name { get; set; } [JsonIgnore] public string Sex { get; set; }}
Custom serialization names
public class Person { public int Age { get; set; } public string Name { get; set; } [JsonProperty(PropertyName = "SepcialSex")] public string Sex { get; set; }}
Parse literal strings
Firstly, a JSON object is constructed to describe a teacher entity, which contains the basic attributes of the teacher (name, age), the class attributes of the managed class, and the array of students of the managed class
{" name ":" zhang ", "age" : "10", "classroom" : {" name ":" class 1, grade 1 ", "desc" : "describe"}, "students" : [{" age ":" nickname "}, {" age ": "Little Red"}]}
Next we use NewtonSoftJSON to parse the string and get the information we need
string jsonStr = "{\" name \ ": \" zhang SAN \ ", \ "age \" : \ "10 \", \ "classroom \" : {\ "name \" : \ "class 1, grade 1 \", \ "desc \" : \ \ "described"}, \ "students \" : [{\ "name \" : \ "name \"}, {\ "n Ame \ ", \ "little red \}]"} "; JObject jObject = JObject.Parse(jsonStr);
Get name
Console.WriteLine("name is " + jObject["name"].ToString());
Console.WriteLine("age is " + jObject.GetValue("age").ToString());
Get class information
Console.WriteLine("classroom name is " + jObject["classroom"]["name"]);
Get student information
JArray jArray = JArray.Parse(jObject["students"].ToString());
foreach (var arr in jArray)
{
JObject jObj = JObject.Parse(arr.ToString());
string name = jObj["name"].ToString();
Console.WriteLine("student name is " + name);
}
Determine whether the key exists
// Key does not exist if (JObject.Property (" ABC ")! = null) { Console.WriteLine(jObject.Property("abc").Value.ToString()); } else {console. writeLine (" Current Key does not exist "); }
WebAPI under the application
Nullable<> : Nullable<> : Nullable<> : Nullable<> : Int? , a DateTime? , NULL value field serialization return values are NULL, the front corresponding field assignment need to do NULL value judgment, how to replace NULL globally.
.NET Core processing WebAPI JSON returns an annoying null for NULL
conclusion
The use of JSON goes far beyond this. The author just sorted out the common problems in his work up to now, and added new contents in the future, constantly updating and optimizing the article.
The END
This article ends here, hope to have help to you 😃
More wonderful technical articles summarized in my public number programmer toolset, continue to update, welcome to subscribe to the collection.
If there are any questions or suggestions, you can communicate more, the original article, the writing is limited, the talent is shallow, if there is something wrong in the article, wang told.
Welfare kyi
Public number background reply: “pay attention to gift package”, access to the value of 5K video learning resources
Public number background reply: “skill map”, send you a most complete developer skill map