JObject 用法 、JProperty 用法、JArray 用法 Linq 转 Json
转:
//遇到"{ }",就要用JObject了 , //遇到双引号"",就用JProperty //遇到键值对了,就要用JProperty了。===》遇到双引号"",就用JProperty //数组的问题,就是要用JArray jArray = new JArray(); jArray.Add(jjj); //注意注意:这里全程都是()小括号,只有小括号!!!!! //可以根据Json的格式写构造Jobject和JProperty var Body = new JObject( new JProperty("心脏", "一个"), new JProperty("心房", "2只")); JArray array = new JArray(); array.Add(Body); var obj = new JObject( new JProperty("Head","头"), new JProperty("Body", new JObject(new JProperty("肚皮","一张"), new JProperty("手", "2只"), new JProperty("后背", "一张"), new JProperty("内部构造",array))), new JProperty("Foot", "脚") ); Console.WriteLine(JsonConvert.SerializeObject(obj));
JObject 用法 、JProperty 用法、JArray 用法 Linq 转 Json
Json to JObject转换的使用方法
1、使用LINQ to JSON前,需要引用Newtonsoft.Json的dll和using Newtonsoft.Json.Linq的命名空间。LINQ to JSON主要使用到JObject, JArray, JProperty和JValue这四个对象,
JObject用来生成一个JSON对象,简单来说就是生成”{}”,
JArray用来生成一个JSON数组,也就是”[]”,
JProperty用来生成一个JSON数据,格式为key/value的值,
而JValue则直接生成一个JSON值
来点干货
BLL.addeditemgroup addeditemgroupBLL = new BLL.addeditemgroup(); DataSet ds = addeditemgroupBLL.GetEditDataList(Id); JArray ja = new JArray(); if (ds.Tables[0].Rows.Count>0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { JObject jo = new JObject( new JProperty("id",ds.Tables[0].Rows[i]["id"]), new JProperty("bigName", ds.Tables[0].Rows[i]["bigName"])); ja.Add(jo); } } JObject joReturn = new JObject(new JProperty("data", ja), new JProperty("errEng", "Operation succeeded"), new JProperty("err","")); return joReturn.ToString(); 或者: JObject joReturn = new JObject(new JProperty("data", new JObject(new JProperty("list",ja))), new JProperty("rsm",1), new JProperty("errno",""), new JProperty("errEng", "Operation succeeded"), new JProperty("err",""));
最终生成Json 格式输出。
var Orderline = new JArray(); string detail = jdata["detail"].ToString(); JArray jArray = JArray.Parse(detail); var OrderDeatil = jArray.Select(l => new JObject { new JProperty("quantity", l["quantity"].ToString()) }); Orderline.Add(OrderDeatil); //封装JSON JObject joo = new JObject( new JProperty("xxxx", "") ); JArray jaa = new JArray(); jaa.Add(joo); var objj = new JObject { {"Name","xxx"}, {"Company", new JObject { {"WHSE", "xxxx"} } }, {"pOs",jaa} };
人各有命,上天注定,有人天生为王,有人落草为寇。脚下的路,如果不是你自己的选择,那么旅程的终点在哪,也没人知道。你会走到哪,会遇到谁,都不一定。