WebApi 简单使用 JObject,可以省掉自定义的class类

post提交的json数据:{"name":"Jason", "age":18, "color":"blue"}
using Newtonsoft.Json.Linq;
     
[HttpPost("testpost")] public string TestPost([FromBody] Object input) { var newobj = JObject.FromObject(input); newobj.Add(new JProperty("qq", "qqai")); string v_name = newobj["name"]?.ToString(); var v_age = newobj.GetValue("age"); string v_color = newobj.Property("color")?.Value.ToString(); var getqkey = newobj.GetValue("qq").ToString(); return $"name:{v_name},age:{v_age},color:{v_color},qq:{getqkey}"; }
显示:name:,age:,color:,qq:qqai

{"name":"Jason", "age":18, "color":"blue","book":[{"id":"1001","bname":"三国演义"}]}   稍微复杂的对象数据

 

posted @ 2024-08-15 16:15  天天向上518  阅读(23)  评论(0编辑  收藏  举报