Note that there are some explanatory texts on larger screens.

plurals
  1. POjson.net check if children exists
    text
    copied!<p>I have some json and parse the content using json.net. Firstly I use the JToken and split the fields from a post into a list of objects and then read the values from each. The problem is that some of these objects sometimes have the fields and sometimes dont and not sure how to prevent a nullexception error from being called. Here is the json sample:</p> <pre><code>{"id": "dfdksfjaisdfdsjfksdjdfsdafernd"} {"body": "test data for parsing"} {"object": { "summary":"hello test data you trouble maker", "id" :"problem stuff", "link": "http://testdata.com", "objecttype": "link" }} {"user": { "name": "Joe Somebody", "age":"43", "location": { "name": "outer space"}, } } </code></pre> <p>Now in some posts the location field is not present in user object which results in a null error in my code as shown below and also sometime the object object is in a different format as follows:</p> <pre><code> {"id": "dfdksfjaisdfdsjfksdjdfsdafernd"} {"body": "test data for parsing"} {"object": { "object": {"summary":"hello test data you trouble maker", "id" :"problem stuff", "link": "http://testdata.com", "objecttype": "link" } }} {"user": { "name": "Joe Somebody", "age":"43", } } </code></pre> <p>now if my code is as follows:</p> <pre><code> Dim results As List(Of JToken) = jobj.Children().ToList If results.Count = 0 Then Exit Sub End If For Each item As JProperty In results item.CreateReader() If item.Name = "id" Then id = item.Value End If If item.Name = "actor" Then author = item.Value("name").ToString location = item.Value("location").Item("name").ToString age = item.Value("age").ToString End If If item.Name = "object" Then htext = item.Value("summary").ToString csubject = item.Value("objectType").ToString End If Next </code></pre> <p>Then if location and object are different this code will not work. so how can I check to see if the location field is present and also if the object path is different with another object within it before the summary field.</p> <p>EDIT:</p> <p>Ok thought I figured it out with</p> <pre><code> If Not item.Value("location") Is Nothing Then bla bla bla </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload