C# json对象中的迭代问题
我正在努力处理一个json对象 我用下面的代码创建对象(RO),效果很好C# json对象中的迭代问题,c#,json,linq,iteration,C#,Json,Linq,Iteration,我正在努力处理一个json对象 我用下面的代码创建对象(RO),效果很好 string reply = @"" + client.UploadString(url, "POST", LS_json); RootObject RO = new RootObject(); RO = JsonConvert.DeserializeObject<RootObject>(reply); 问题是,当cnt_V==4并且“指向”最后一个条目属性[cnt_V]时,LS_ande将按照假定的(=“K
string reply = @"" + client.UploadString(url, "POST", LS_json);
RootObject RO = new RootObject();
RO = JsonConvert.DeserializeObject<RootObject>(reply);
问题是,当cnt_V==4并且“指向”最后一个条目属性[cnt_V]时,LS_ande将按照假定的(=“KEY_值”)填充
但是foreach会再次迭代(cnt_V==5),这里没有问题,但是当它被分配给LS_ande时,它会转储(当然是因为没有包含cnt_V==5的数据的条目)
我不明白怎么了。请对我温柔一点,随时询问更多信息。
提前感谢。由于我没有数据,所以无法明确回答,但我首先要说的是:
//take out the long and lengthy parts to make the rest clearer
//I see there are two things here, intentional?
var something = RO.hits.hits[0]._source.Biz.Rel[cnt_I].org[cnt_III].mem[cnt_IV].attributter;
var somethingElse = RO.hits.hits[0]._source.Biz.Rel[cnt_I].mem[cnt_III].xsData[cnt_IV].attributes;
cnt_V = 0;
//Here, you are iterating over something[cnt_V].type, but also change cnt_V in the body.
//Are you sure this is correct?
foreach (object obj_attributtertype in something[cnt_V].type)
{
if (Convert.ToString(somethingElse[cnt_V].type) == "KEY_VALUES")
{
LS_ande = "" + Convert.ToString(somethingElse[cnt_V].values[0].value);
}
cnt_V++;
}
从这个角度看,这是我在黑暗中的刺。在
某物中的项的Count()
上使用进行迭代
var something = RO.hits.hits[0]._source.Biz.Rel[cnt_I].org[cnt_III].mem[cnt_IV].attributter;
var somethingElse = RO.hits.hits[0]._source.Biz.Rel[cnt_I].mem[cnt_III].xsData[cnt_IV].attributes;
for (var cnt_V = 0; cnt_V < something.Count(); ++cnt_V)
{
if (Convert.ToString(somethingElse[cnt_V].type) == "KEY_VALUES")
{
LS_ande = "" + Convert.ToString(somethingElse[cnt_V].values[0].value);
}
cnt_V++;
}
var something=RO.hits.hits[0]。_source.Biz.Rel[cnt_I].org[cnt_III].mem[cnt_IV].attributeter;
var somethingElse=RO.hits.hits[0]。_source.Biz.Rel[cnt_I]。mem[cnt_III]。xsData[cnt_IV]。属性;
for(var cnt_V=0;cnt_V
在像RO.hits.hits[0].\u source.Vrvirksomhed.deltagerRelation[cnt_I].Organizationer[cnt_III].medlemsData[cnt_IV].Attributer[cnt_V]这样的长路径上迭代。如果中间对象之一为空,则类型将导致错误。您必须测试中间的对象是否为空。@jdweng已经尝试过了。它没有做到这一点:if(RO.hits.hits[0].\u source.Biz.Rel[cnt_I].org[cnt_III].mem[cnt_IV].attributes[cnt_V].values[0].value!=null){LS_and=“”+Convert.ToString(RO.hits.hits[0].\u source.Biz.Rel[cnt_I].org[cnt_III].mem[cnt_IV].attributes[cnt_V].values[0].value);}RootObject RO=new RootObject()如果在下一行中为RO分配不同的内容,则代码>是一种浪费。而不是考虑<代码> var ROR= jSONTRORES.DRIIALIZEY对象(答复);<如果“dumps”是指存在NullReferenceException
,那么这是@crashmstr的重复。而是:System.ArgumentOutOfRangeException
var something = RO.hits.hits[0]._source.Biz.Rel[cnt_I].org[cnt_III].mem[cnt_IV].attributter;
var somethingElse = RO.hits.hits[0]._source.Biz.Rel[cnt_I].mem[cnt_III].xsData[cnt_IV].attributes;
for (var cnt_V = 0; cnt_V < something.Count(); ++cnt_V)
{
if (Convert.ToString(somethingElse[cnt_V].type) == "KEY_VALUES")
{
LS_ande = "" + Convert.ToString(somethingElse[cnt_V].values[0].value);
}
cnt_V++;
}