简体   繁体   中英

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

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"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM