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