I'm currently working on an ASP.NET Web API .NET Framework 4.7.2. I try to alter some JSON data in my service class. I try to add a new object after every 2nd object in my JArray.
我目前正在开发ASP.NET Web API .NET Framework 4.7.2。我尝试在服务类中更改一些JSON数据。我尝试在JArray中的每个第二个对象之后添加一个新对象。
I thought about manipulating the JSON data, rather then concrete objects because the received data will most likely be dynamic data. I'm using the library JObject, but I'm getting some error without any real exception messages.
我考虑过操纵JSON数据,而不是具体对象,因为接收到的数据很可能是动态数据。我正在使用库JObject,但我收到一些错误,没有任何真正的异常消息。
My received JSON structure looks like that:
我收到的JSON结构如下所示:
{ "data" : [
{"IsPlaceholder": 0, "Name" : "Test1", "Size" : 2 },
{"IsPlaceholder": 0, "Name" : "Test2", "Size" : 3 },
{"IsPlaceholder": 0, "Name" : "Test3", "Size" : 1 }
]}
My service class looks like that:
我的服务类看起来像这样:
public class MyService : IMyService
{
public async Task<JObject> UpdateInformationAsync(JObject coolData)
{
// Random placeholder, new placeholder object after 2nd
var placeholder = JObject.FromObject(new PlaceholderVm());
var cnt = 0;
foreach (JObject c in coolData["data"] as JArray)
{
if (cnt % 2 == 0)
{
coolData["data"][cnt].AddAfterSelf(placeholder);
}
cnt++;
}
return coolData;
}
}
My placeholder view model looks like that:
我的占位符视图模型如下所示:
public class PlaceholderVm
{
public int IsPlaceholder => 1;
public string Name => "Placeholder";
public float Size { get; set; } = 0;
}
When I try to add a placeholderVm to my JArray, it works fine the first time, but on the 2nd iteration it throws an error without exception message.
当我尝试将placeholderVm添加到我的JArray时,它第一次正常工作,但在第二次迭代时,它会抛出一个没有异常消息的错误。
Do you know how I can add a new JObject on nth position to my JArray?
你知道如何在我的JArray的第n个位置添加一个新的JObject吗?
Thank you!
1 个解决方案
#1
0
This is because you are mutating the underlying collection while looping through it in the foreach
. This is the reason you'll often times see folks initialize a new List<T>
when doing operations like this, to avoid this error.
这是因为你在foreach中循环遍历它时正在改变底层集合。这就是为什么你经常会看到人们在做这样的操作时初始化一个新的List
It actually yields this exception :
它实际上产生了这个异常:
Run-time exception (line 21): Collection was modified; enumeration operation may not execute.
运行时异常(第21行):修改了集合;枚举操作可能无法执行。
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
System.InvalidOperationException:集合已被修改;枚举操作可能无法执行。
The easiest way around it is to simply create a new collection and place things where you want them to go. In your case, this may look like :
最简单的方法是简单地创建一个新的集合并将东西放在你想要的位置。在您的情况下,这可能看起来像:
var jObject = new JObject();
JArray jArray = new JArray();
foreach (JObject c in coolData["data"] as JArray)
{
jArray.Add(c);
if (cnt % 2 == 0)
{
jArray[jArray.Count - 1].AddAfterSelf(placeholder);
}
cnt++;
}
jObject.Add("data", jArray);
Here's a .NET Fiddle
这是一个.NET小提琴
#1
0
This is because you are mutating the underlying collection while looping through it in the foreach
. This is the reason you'll often times see folks initialize a new List<T>
when doing operations like this, to avoid this error.
这是因为你在foreach中循环遍历它时正在改变底层集合。这就是为什么你经常会看到人们在做这样的操作时初始化一个新的List
It actually yields this exception :
它实际上产生了这个异常:
Run-time exception (line 21): Collection was modified; enumeration operation may not execute.
运行时异常(第21行):修改了集合;枚举操作可能无法执行。
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
System.InvalidOperationException:集合已被修改;枚举操作可能无法执行。
The easiest way around it is to simply create a new collection and place things where you want them to go. In your case, this may look like :
最简单的方法是简单地创建一个新的集合并将东西放在你想要的位置。在您的情况下,这可能看起来像:
var jObject = new JObject();
JArray jArray = new JArray();
foreach (JObject c in coolData["data"] as JArray)
{
jArray.Add(c);
if (cnt % 2 == 0)
{
jArray[jArray.Count - 1].AddAfterSelf(placeholder);
}
cnt++;
}
jObject.Add("data", jArray);
Here's a .NET Fiddle
这是一个.NET小提琴