さまざまな場所から返されるJObjectがいくつかありますが、それらはすべて同じプロパティを持っています。これを1つの大きなjObjectに連結/マージする必要があります。これは可能ですか?どうすればそれを実行できますか?
私はそれが個々のオブジェクトとすべて同じ特性を持っていることを望みます。例えば。
jObject1 = { "data": [{"name": "foo","id": "1234" }]};
jObject2 = {"data": [{ "name": "foo2", "id": "5678" }]};
このような結果になります。
jobject3 = { "data": [{ "name": "foo", "id": "1234"}, { "name": "foo2", "id": "5678" }]};
私はC#でコーディングしていますが、これまでに考えたことは、このようなものだけではありません。どうやって始めたらいいのかわからず、何もできません。
jobject3 = jObject1.Concat(jObject2);
各オブジェクトを手動でループして、新しいオブジェクトを作成しようとしています。近いと思いますが、2番目の項目(oAlldepartment.Add)を追加すると、「Newtonsoft.Json.Linq.JObjectにプロパティを追加できません。同じ名前のプロパティがオブジェクトに既に存在します」というエラーが発生し続けます。
動的dynObj=JsonConvert.DeserializeObject(people); foreach(dynObj.dataのvar item){string id = item.id; 文字列名=item.name;
department = getdepartment(id);
JObject oDepartment = new JObject();
try
{
if (!String.IsNullOrEmpty(department))
oDepartment = JObject.Parse(department);
}
catch (Exception ex)
{
}
JArray departmentArray = new JArray();
if (oDepartment != null)
{
foreach (var x in oDepartment["data"].Children())
{
try
{
JObject departmentObject = new JObject();
((JObject)departmentObject).Add(new JProperty("name", x["name"]));
((JObject)departmentObject).Add(new JProperty("department", new JObject(new JProperty("name", x["department"]["name"]))));
((JObject)departmentObject).Add(new JProperty("hire_date", x["hire_date"]));
((JObject)departmentObject).Add(new JProperty("description", x["description"]));
departmentArray.Add(departmentObject);
}
catch (Exception ex)
{
}
((JObject)x).Add(new JProperty("itemtype", "post"));
}
try
{
oAlldepartment.Add(new JProperty("", new JArray(departmentArray)));
}
catch (Exception ex)
{
}
}
}
ありがとう、
ロンダ