JObject、JArray对JSON操作

  增删改查:

复制代码
var cardJson = "['身份证','银行卡','门禁卡']";
            var cardJArray = JArray.Parse(cardJson);
        </span><span style="color: #0000ff;">var</span> p = <span style="color: #0000ff;">new</span> Person { Name=<span style="color: #800000;">"</span><span style="color: #800000;">fan</span><span style="color: #800000;">"</span>,Age=<span style="color: #800080;">12</span>,Dog=<span style="color: #0000ff;">new</span> Dog { Name=<span style="color: #800000;">"</span><span style="color: #800000;">奶牛</span><span style="color: #800000;">"</span><span style="color: #000000;">} };
        </span><span style="color: #0000ff;">var</span> pJson =<span style="color: #000000;"> JsonConvert.SerializeObject(p);
        
        JObject jobj </span>=<span style="color: #000000;"> JObject.Parse(pJson);
        
        </span><span style="color: #008000;">//</span><span style="color: #008000;">添加属性</span>
        jobj[<span style="color: #800000;">"</span><span style="color: #800000;">NickName</span><span style="color: #800000;">"</span>] = <span style="color: #800000;">"</span><span style="color: #800000;">fan</span><span style="color: #800000;">"</span><span style="color: #000000;">;
        jobj[</span><span style="color: #800000;">"</span><span style="color: #800000;">Wifes</span><span style="color: #800000;">"</span>] = <span style="color: #0000ff;">new</span> JArray(<span style="color: #800000;">"</span><span style="color: #800000;">rose</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">lisa</span><span style="color: #800000;">"</span><span style="color: #000000;">);
        ((JArray)jobj[</span><span style="color: #800000;">"</span><span style="color: #800000;">Wifes</span><span style="color: #800000;">"</span>]).Add( <span style="color: #800000;">"</span><span style="color: #800000;">july</span><span style="color: #800000;">"</span><span style="color: #000000;">);
        jobj[</span><span style="color: #800000;">"</span><span style="color: #800000;">Cards</span><span style="color: #800000;">"</span>] =<span style="color: #000000;"> cardJArray;
        </span><span style="color: #008000;">//</span><span style="color: #008000;">修改属性</span>
        jobj[<span style="color: #800000;">"</span><span style="color: #800000;">Name</span><span style="color: #800000;">"</span>] = <span style="color: #800000;">"</span><span style="color: #800000;">li</span><span style="color: #800000;">"</span><span style="color: #000000;">;
        jobj[</span><span style="color: #800000;">"</span><span style="color: #800000;">Age</span><span style="color: #800000;">"</span>] = <span style="color: #800080;">9</span><span style="color: #000000;">;
        jobj[</span><span style="color: #800000;">"</span><span style="color: #800000;">Dog</span><span style="color: #800000;">"</span>][<span style="color: #800000;">"</span><span style="color: #800000;">Name</span><span style="color: #800000;">"</span>] = <span style="color: #800000;">"</span><span style="color: #800000;">阿黄</span><span style="color: #800000;">"</span><span style="color: #000000;">;
        ((JArray)jobj[</span><span style="color: #800000;">"</span><span style="color: #800000;">Wifes</span><span style="color: #800000;">"</span>])[<span style="color: #800080;">0</span>] = <span style="color: #800000;">"</span><span style="color: #800000;">roses</span><span style="color: #800000;">"</span><span style="color: #000000;">;
        </span><span style="color: #008000;">//</span><span style="color: #008000;">插入属性</span>
        jobj.Property(<span style="color: #800000;">"</span><span style="color: #800000;">Name</span><span style="color: #800000;">"</span>).AddBeforeSelf(<span style="color: #0000ff;">new</span> JProperty(<span style="color: #800000;">"</span><span style="color: #800000;">ID</span><span style="color: #800000;">"</span>, <span style="color: #800080;">1</span><span style="color: #000000;">));
        </span><span style="color: #008000;">//</span><span style="color: #008000;">删除属性</span>
        jobj.Property(<span style="color: #800000;">"</span><span style="color: #800000;">Age</span><span style="color: #800000;">"</span><span style="color: #000000;">).Remove();
        ((JArray)jobj[</span><span style="color: #800000;">"</span><span style="color: #800000;">Wifes</span><span style="color: #800000;">"</span>])[<span style="color: #800080;">2</span><span style="color: #000000;">].Remove();
        </span><span style="color: #008000;">//</span><span style="color: #008000;">查找
        </span><span style="color: #008000;">//</span><span style="color: #008000;">遍历属性</span>
        <span style="color: #0000ff;">var</span> props =<span style="color: #000000;"> jobj.Children().Values();
        </span><span style="color: #0000ff;">foreach</span> (<span style="color: #0000ff;">var</span> prop <span style="color: #0000ff;">in</span><span style="color: #000000;"> props)
        {
            </span><span style="color: #0000ff;">if</span> (!(prop <span style="color: #0000ff;">is</span> JObject) &amp;&amp; !(prop <span style="color: #0000ff;">is</span><span style="color: #000000;"> JArray))
            {
                Console.WriteLine(prop.ToString());
            }               
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">遍历数组</span>
        <span style="color: #0000ff;">var</span> cardList = ((JArray)jobj[<span style="color: #800000;">"</span><span style="color: #800000;">Cards</span><span style="color: #800000;">"</span><span style="color: #000000;">]);
        </span><span style="color: #0000ff;">foreach</span> (<span style="color: #0000ff;">var</span> card <span style="color: #0000ff;">in</span><span style="color: #000000;"> cardList)
        {
            Console.WriteLine(card.ToString());
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">强类型读取</span>
        Console.WriteLine(jobj.Value&lt;<span style="color: #0000ff;">int</span>&gt;(<span style="color: #800000;">"</span><span style="color: #800000;">ID</span><span style="color: #800000;">"</span><span style="color: #000000;">)); 
        </span><span style="color: #0000ff;">var</span> p2 = jobj.ToString();</pre>
复制代码

 

对象->JObject->JSON:

复制代码
var json = new JObject(
        new JProperty("status", result.Status.ToString()),
        new JProperty("results", new JObject(result.Entries.Select(pair =>
            new JProperty(pair.Key, new JObject(
                new JProperty("status", pair.Value.Status.ToString()),
                new JProperty("description", pair.Value.Description),
                new JProperty("data", new JObject(pair.Value.Data.Select(
                    p => new JProperty(p.Key, p.Value))))))))));

var str = json.ToString();

复制代码

 

JObject合并:

jObj.Merge(JObject.Parse(userTranslate.ValueJSON));

 

JObject查找:

jObject.SelectToken($".{path}");//根据路径查找

 

posted @ 2020-07-31 11:32  奋斗的中年人哈哈哈  阅读(273)  评论(0编辑  收藏  举报