Newtonsoft.Json use to return Null value or throw exception finishing

            string jsonstr_right = "{'k':'v','ks':{'kk':'vv','kk2':'vv2'},'c':2}";
            string jsonstr_wrong = "{dkfkd}";
            
            //JObject jobj1 = JObject.Parse(jsonstr_wrong);//Error when the string is not in json format
            JObject jobj = JObject.Parse(jsonstr_right);
            //JObject jobj = new JObject(jsonstr_wrong);//Error when the string is not in json format
            //JObject jobj = new JObject(jsonstr_right);//When the string is in json format, it also reports an error, drunk
            //JObject jobj = new JObject(new { k="v", Name="name",Age=20});//Passing an object also reports an error, I really don't know what is the use of this method
            //object result = jobj["kk"];//Return null when key does not exist
            //string result = jobj.Value<string>("kk");//Return null when the key does not exist
            //string result = jobj.Value<string>("cc");//Return null when key does not exist
            //int result = jobj.Value<int>("cc");//Return 0 when the key does not exist
            //int result = jobj.Value<int>("k");//Error when value cannot be converted to specified type
            //object result = jobj["k"]["kk"];//An error will be reported if any key does not exist
            //object result = jobj.Value<JToken>("k").Value<string>("kk");//An error will be reported if any key does not exist
            //JObject//JSON object
            //JArray//Json array
            //JToken// is used to store the result of Linq to JSON query
            //JProperty//The property in the object, in the form of "key/value"

            string result = jobj.Value<string>("k");
            Console.WriteLine(result);
            Console.ReadKey();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763406&siteId=291194637