Dealing with a JSON object's property called "private" in C# with Newtonsoft's JSON.NET

StackOverflow https://stackoverflow.com/questions/23470040

  •  15-07-2023
  •  | 
  •  

Вопрос

I am trying to parse a json string returned from the xively feed from an Air Quality Egg. One of the properties says whether the xively feed is public or private. The property is called private and takes the string value "true" or "false". To get data from the feed I am calling xively's Historical Data REST API which successfully returns me valid JSON. I am then using JSON.NET to parse the JSON in C#. My parsing starts thus:

dynamic historicalDatapoints  = JValue.Parse(jsonString) as JObject;
if (historicalDatapoints != null)
{
    var id = historicalDatapoints.id;
    var title = historicalDatapoints.title.ToString();
    var privacy = bool.Parse(historicalDatapoints.private.ToString())
    // More parsing
}

I have a problem with that last line of code. C# will not let me refer to the property called "private". Here's the corresponding (redacted) JSON:

{
    "id": 000000843,
    "title": "Blah Road Egg 02",
    "private": "false",
    //...
}

Using JSON.NET how should I parse out the property private?

Это было полезно?

Решение

Try historicalDatapoints.@private to access that value. If that doesn't work then you could also try historicalDatapoints["private"]

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top