This is the 7th day of my participation in Gwen Challenge
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 common problem in the beginning. NewtonsoftJson is a set of json processing encapsulation classes for c#, which can help us efficiently and easily handle json.
NetonSoftJson provides the complete usage Documentation at Json.NET Documentation
Different usage scenarios of NetonSoftJson
Common methods serialization and deserialization
These are probably the two things we use the most
JsonConvert. DeserializeObject < T > (string json) / / deserialization JsonConvert. SerializeObject (object obj) / / serializationCopy the code
Advanced features
See the article newtonsoft.json for advanced usage
Some fields do not want to be deserialized
Use the JsonIgnore feature tag
public class Person { public int Age { get; set; } public string Name { get; set; } [JsonIgnore] public string Sex { get; set; }}Copy the code
Custom serialization name
public class Person { public int Age { get; set; } public string Name { get; set; } [JsonProperty(PropertyName = "SepcialSex")] public string Sex { get; set; }}Copy the code
Parse literal strings
First build a JSON object, describing a teacher’s entity, which contains the basic attributes of the teacher (name, age), the attributes of the managed class, the managed class student array
{" name ":" zhang ", "age" : "10", "classroom" : {" name ":" class 1, grade 1 ", "desc" : "describe"}, "students" : [{" age ":" nickname "}, {" age ": "Little red"}]}Copy the code
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);Copy the code
Get name
Console.WriteLine("name is " + jObject["name"].ToString());
Console.WriteLine("age is " + jObject.GetValue("age").ToString());
Copy the code
Get class information
Console.WriteLine("classroom name is " + jObject["classroom"]["name"]);
Copy the code
Access to 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);
}
Copy the code
Check 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 "); }Copy the code
Application under WebApi
Nullable<> = Nullable<> = Nullable<> = int? , a DateTime? , the value returned by the null value field serialization is null, the foreground corresponding field assignment needs to perform null value judgment, how to replace null globally.
.NET Core handles WebAPI JSON and returns an annoying null null
conclusion
The use of Json is far more than that. The author just sorted out the common problems in his work so far, and will gradually add new content in the future, and constantly update and optimize the article.
The END
The end of this article, I hope to help you 😃
More exciting technical articles summary in my public number programmer tool set, continue to update, welcome to pay attention to the subscription collection.
If there are any questions or suggestions, you can exchange more original articles, writing is limited, talent and learning is shallow, if there is something wrong in the article, hope to inform.
Welfare kyi
Public account background reply: “pay attention to gift package”, get the value of 5K video learning resources
Public number background reply: “skill map”, send you a most complete developer skill map