Reputation: 10552
Hey all I have the following code:
try {
foreach (var x1 in JObject.Parse(json1)) {
string name = x1.Key;
JToken value = x1.Value;
There are no visible errors with the above code. However, once I run the application and it gets to that point it errors and says:
'Newtonsoft.Json.Linq.JProperty' does not contain a definition for 'Key'
So whats going on here. My VB.net code works like the above:
Try
For Each x1 In JObject.Parse(json1)
Dim name As String = x1.Key
Dim value As JToken = x1.Value
Upvotes: 1
Views: 2037
Reputation: 12854
JProperty
(you get JPropertys by iterating over a JObject) inherits from JContainer
, which inherits from JToken
. JToken
implements IDynamicMetaObjectProvider
.
This makes it essentially a dynamic
object, which allows you to access anything on it at compile time. That's why you dont get a compiler error.
At runtime the DLR discovers that there is no Key
property on x1
and throws that exception.
If you want to get the property name, you should use the Name
property.
foreach (var x1 in JObject.Parse(json1)) {
string name = x1.Name;
JToken value = x1.Value;
Upvotes: 1