0

I use an API which returns JSON. I'm having trouble with accessing elements when they are not in an array.

My JSON looks like this:

On the JSON of 2, I can access the elements with

dataJson.Storingen.Ongepland.Storing.@elementName@

However, when I use the JSON of 1, I get the following exception:

Additional information: Newtonsoft.Json.Linq.JProperty does not contain a definition for Traject
grg
6,0744 gold badges40 silver badges57 bronze badges
asked Apr 7, 2015 at 9:25
3
  • Check your JSON again, It seems the second entry is actually nested within the first entry, hence why it is inaccessible. (Unless I'm going crazy) -- Also shouldn't it be dataJson.Storingen.Gepland.Storing.@elementName@ ? -- Feel free to post the original JSON on pastebin and I'll look at it. Commented Apr 7, 2015 at 9:32
  • I made mistake. Number 2 works but 1 doesn't, but I still don't know how to fix it this is the json where i can't access the elements: pastebin.com/NsL2txnx this is the one where it works pastebin.com/eFc1JUWv Commented Apr 7, 2015 at 9:48
  • Please see "Should questions include "tags" in their titles?", where the consensus is "no, they should not"! Commented Apr 7, 2015 at 10:44

3 Answers 3

2

The structure of second json is different about the first. In second case, you should use dataJson.Storingen.Ongepland.Storing[0].@property to access the property that you want.

But if you want to roll up in your array, just to use a for

answered Jul 24, 2017 at 16:01

Comments

1

The two JSONs on that picture are not similar in architecture. The first one has a nested "Storing" object, that has several properties, but in the second case, "Storing" became an array of objects. Is it possible that your object model that you're trying to map to tries to parse this array as a single object?

If so, then I think you need to change the type of "Storing" in your model to an array. You will be able to get the elements then like this: dataJson.Storingen.Ongepland.Storing[0].@elementName@

answered Apr 7, 2015 at 9:34

Comments

0

I know this is an old post, but I thought maybe I could just share my thoughts on this question number 1 I believe, just in case someone from the future saw this.

1st. Create two classes as such:

public Storing Test {get; set;}
public class Storing
{
 public string id {set; get;}
 public string Traject {set; get;}
 public string Periode {set; get;}
}

2nd. In the main program, deserialize the JSON and call the object as such

 TestCase list = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<TestCase>(*your web API uri*);
 Console.WriteLine ("id:{0}, Traject{1}, Periode{2}", list.Id, list.Traject, list.Periode);
answered Jul 24, 2017 at 15:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.