1

Im struggling with a json object.

I create the object (RO) with the following code and that Works fine.

string reply = @"" + client.UploadString(url, "POST", LS_json);
RootObject RO = new RootObject();
RO = JsonConvert.DeserializeObject<RootObject>(reply);

RO now contains all the data I have recieved through the json search.

Now, when iterating through the object the foreach iterate one more than (RO) contains:

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++; 
}

The thing is that when cnt_V == 4 and "points" to the last entry attributes[cnt_V] then LS_ande is filled as supposed (=="KEY_VALUES").

But then the foreach iterates again (cnt_V == 5), no problem here, but when it is assigned to LS_ande then it dumps (of cource because there is no entry with data for cnt_V == 5).

I dont understand whats wrong. Please be gentle with me and feel free to ask for further information. Thanks in advance.

Lars Hansen
  • 155
  • 1
  • 2
  • 16
  • Iterating through a long path like RO.hits.hits[0]._source.Vrvirksomhed.deltagerRelation[cnt_I].organisationer[cnt_III].medlemsData[cnt_IV].attributter[cnt_V].type will cause errors if one of the middle objects is null. You have to test that none of the middle objects are null. – jdweng Nov 29 '16 at 12:27
  • @jdweng Already tried that. It didn't do the trick: if (RO.hits.hits[0]._source.Biz.Rel[cnt_I].org[cnt_III].mem[cnt_IV].attributes[cnt_V].values[0].value != null) { LS_ande = "" + Convert.ToString(RO.hits.hits[0]._source.Biz.Rel[cnt_I].org[cnt_III].mem[cnt_IV].attributes[cnt_V].values[0].value); } – Lars Hansen Nov 29 '16 at 19:54
  • 1
    `RootObject RO = new RootObject();` is a waste if you then in the next line assign something different to `RO`. Consider instead `var RO = JsonConvert.DeserializeObject(reply);` – crashmstr Nov 29 '16 at 20:05
  • And if by "dumps" you mean that there is a `NullReferenceException`, then this is a duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – crashmstr Nov 29 '16 at 20:07
  • @crashmstr Its not. Instead its: System.ArgumentOutOfRangeException – Lars Hansen Nov 29 '16 at 21:28
  • then you'll need to check your indexer variables to make sure they are not out of range. – crashmstr Nov 29 '16 at 21:39
  • @crashmstr My problem is that: When I do the iteration: foreach (object obj_attributtertype in RO.hi....... Then there are four elements in the object. But it keeps iterating beyond the fourth element, although there arent any more elements in the list. – Lars Hansen Nov 29 '16 at 22:06
  • `foreach` will not throw that exception. Step through in your debugger and examine everything in the `[]` in the `foreach` line and its body. One of the `[]` is out of bounds. – crashmstr Nov 29 '16 at 22:22
  • @crashmstr . Indeed. That was what I wanted to describe. Allthough I only see 4 elements in the [] then the foreach runs an extra (where its out of boundary) iteration. – Lars Hansen Nov 30 '16 at 06:13
  • I *don't* have your data, so I *can't* debug this myself, but `RO.hits.hits[0]._source.Biz.Rel[cnt_I].org[cnt_III].mem[cnt_IV].attributter[cnt_V].type` is `foreach`ing over `type` **and** you are changing `cnt_V` in the `foreach` body. This seems strange by itself. – crashmstr Nov 30 '16 at 12:38

1 Answers1

1

While I can't answer this definitively because I don't have the data, this is what I would start with:

//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++; 
}

And looking at it that way, here is my stab in the dark.
Iterate with a for over the Count() of items in something

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++; 
}
crashmstr
  • 28,043
  • 9
  • 61
  • 79