Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# json对象中的迭代问题_C#_Json_Linq_Iteration - Fatal编程技术网

C# json对象中的迭代问题

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

我正在努力处理一个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将按照假定的(=“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()是一种浪费。而不是考虑<代码> 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++; 
}