首页 > 解决方案 > 如何将项目附加到退出的 json 数组?下面是我的 json 数组

问题描述

var jsonObject={"data":[{"Id":293,"SourceURL":"//jhthf/hsf/d$","TargetURL":"makemytriip.com","Status":"Failed","UserFriendlyName":"progress","MonthName":"November","StartYear":2020},
{"Id":296,"SourceURL":"//jhthf/hsf/f$","TargetURL":"usermobileapp.com","Status":"Success","UserFriendlyName":"progress","MonthName":"November","StartYear":2020}]}

如何再添加一个参数“entityType”:“Value”,它是该数组中每个项目的对应值?尝试使用 Newtonsoft json( JObject -Add 方法),但它给出以下错误

'对象序列化为数组。需要 JObject 实例。

JObject jo = JObject.FromObject(jsonObject);
                            jo.Add("SiteOwnerDisplayNames", userDisplayNames);

标签: c#.netjson.net

解决方案


错误在 FromObject 上?FromObject 期望在 JObject 中传递。

尝试使用 Json.Parse 将数组转换为 JObject。

JObject jo = JObject.FromObject(Json.Parse(jsonObject));

您拥有的 jsonObject 是使用数组语法创建的,而不是正确创建的 JObject[]


推荐阅读