我正在与一个json对象挣扎。
我使用以下代码创建对象(RO),并且工作正常。
string reply = @"" + client.UploadString(url, "POST", LS_json);
RootObject RO = new RootObject();
RO = JsonConvert.DeserializeObject<RootObject>(reply);
RO现在包含我通过json搜索收到的所有数据。
现在,当迭代对象时,foreach迭代的次数超过(RO)包含:
cnt_V = 0;
foreach (object obj_attributtertype in RO.hits.hits[0]._source.Biz.Rel[cnt_I].org[cnt_III].mem[cnt_IV].attributter[cnt_V].type)
{
if (Convert.ToString(RO.hits.hits[0]._source.Biz.Rel[cnt_I].mem[cnt_III].xsData[cnt_IV].attributes[cnt_V].type) == "KEY_VALUES")
{
LS_ande = "" + Convert.ToString(RO.hits.hits[0]._source.Biz.Rel[cnt_I].mem[cnt_III].xsData[cnt_IV].attributes[cnt_V].values[0].value);
}
cnt_V++;
}
当cnt_V == 4并且“指向”最后一个条目属性[cnt_V]时,则LS_ande被填充为假设(==“KEY_VALUES”)。
但是foreach再次迭代(cnt_V == 5),这里没有问题,但是当它被分配给LS_ande然后它转储(cource,因为没有条目包含cnt_V == 5的数据)。
我不明白什么是错的。请对我温柔,随时可以询问更多信息。 提前谢谢。
答案 0 :(得分:1)
虽然我无法明确回答这个问题,因为我没有数据,但这就是我要开始的:
public string MyJoin(params string[] vars)
{
return "MyFunction(" + string.Join(",", vars) + ");";
}
以这种方式看着,这是我在黑暗中的刺痛
使用//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++;
}
对for
Count()
项进行迭代
something