一个JArray数组由JObject对象组成,往每个JObject对象添加一个键,怎么加呢

goodgame365 2021-02-19 09:47:52
比如我有一个JArray数组 如下
[ {"a":1,"b":2}, {"a":1,"b":2},{"a":1,"b":2},]
现在我想需要加成这样
[ {"a":1,"b":2,"C":3}, {"a":1,"b":2,"C":3}, {"a":1,"b":2,"C":3}]

怎么加呢,请高人指点
...全文
353 3 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
MangGo雨 2021-02-23

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
 var str = "[ {\"a\":1,\"b\":2}, {\"a\":1,\"b\":2},{\"a\":1,\"b\":2}]";
            JArray j = JArray.Parse(str);
            for (int a = 0; a < j.Count; a++)
            {
                j.Add(new JProperty("c", 3));
            }
            string str2 = JsonConvert.SerializeObject(j);
  • 打赏
  • 举报
回复
shawn xia 2021-02-20
            JArray array = new JArray();
            array.Add(JObject.Parse("{ \"a\":1,\"b\":2}"));
            array.Add(JObject.Parse("{ \"a\":1,\"b\":2}"));
            array.Add(JObject.Parse("{ \"a\":1,\"b\":2}"));

            string json = array.ToString();
            //[{ "a":1,"b":2}, { "a":1,"b":2},{ "a":1,"b":2}]
            Console.WriteLine(json);

            array.ToList().ForEach(x=>((JObject)x).Add("c","3"));

            //[ { "a":1,"b":2,"c":3},  { "a":1,"b":2,"c":3}, { "a":1,"b":2,"c":3}]
            Console.WriteLine(array.ToString());
  • 打赏
  • 举报
回复
楠小南 2021-02-19
将对象数组投影到新的集合

   var Jarray = [ {"a":1,"b":2}, {"a":1,"b":2},{"a":1,"b":2},];

   var T = Jarray.Select(a=>new{a.a,a.b,c=3 }).tolist();


  • 打赏
  • 举报
回复
发帖
C#

10.8w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2021-02-19 09:47
社区公告

让您成为最强悍的C#开发者