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}
                           };

 

posted @ 2019-05-10 08:59  ProZkb  阅读(2849)  评论(0编辑  收藏  举报