// sourceJson 记录着部分testjson的itemindex值. for item in sourceJson do begin . // 当第二次进入到这儿时, 由于testjson被删除了一节, // 导致 testjson.item[xq.AsInteger] 不存在报错.. Fjson.Insert(0, testjson.item[xq.AsInteger]); end;
这个问题怎么解决?
procedure TQJson.Insert(AIndex: Integer; AChild: TQJson); begin // 判断有父级. if Assigned(AChild.Parent) then begin if AChild.Parent <> Self then // 如果父级不是自身 AChild.Parent.Remove(AChild) // 将移除子json. else Exit; end; ArrayNeeded(jdtObject); AChild.FParent := Self; AChild.FIgnoreCase := FIgnoreCase; if AIndex <= 0 then AIndex := 0 else if AIndex >= Count then AIndex := Count; FItems.Insert(AIndex, AChild); end;
----------------------------------------------
delphi界写python最强, python界写delphi最强. 写自己的代码, 让别人去运行.
首先,星二,你的标题又调皮了,你是微信软文看多了?! 其次,你要明白,你的itemq 是个对象引用。。为了避免循环引用,在接到新的结点时,必须删除原引用,而你每次for时,原来的ajson的count已经变了,所以变成了bjson只有2个,你还会发现ajson也还剩2个。。。正确的做法是复制一个itemq,建立一个结点,就可以避免你所谓的污染了!。 1、按你的insert,是这样的: // I:=0; // for itemq in ajson do // begin // a := itemq.Clone; // a.Name := itemq.Name; // bjson.Insert(I, a); // I:=I+1; // end; 2、更简单的(由权威人士提供): for itemq in ajson do bjson.add(itemq.name).assign(itemq);
----------------------------------------------
-