[RESOLVED] Newtonsoft help
I'm trying to get a value from a json string using Newtonsoft, but I can't seem to figure this one out.
Here is the json string:
Code:
{
"error_message": "",
"success": true,
"metadata": [
{
"id": "COUNT(*)",
"key": "COUNT(*)",
"name": "COUNT(*)",
"type": "number",
"data": null,
"table_id": "",
"table_name": "",
"base_id": ""
}
],
"is_join_stmt": false,
"results": [
{
"COUNT(*)": 9129
}
]
}
I'm trying to get the value in the results>COUNT(*) node. (9129 in this case)
I have tried the following code, but this gives me an error:
Code:
Dim jsonObject As JObject = JObject.Parse(jsonString)
resultsNode = jsonObject("results")
Dim count As String = resultsNode(0).SelectToken("COUNT(*)").ToString
Any help would be appreciated. Thanks...
Re: [RESOLVED] Newtonsoft help
nbrege, I would suggest that your solution is a bit short sighted. For example, what would happen if your JSON doesn't match the sample?
Also, keep in mind that VB.NET is designed to be an object-oriented programming language. The tools to convert your JSON literal to classes are built-in to Visual Studio, they do not require much effort on your part, and Newtonsoft supports converting JSON literals to classes out of the box.
No question, your solution will work, but I'd highly suggest taking jdelano's suggestion in post #3 and my improvement in post #4.
Re: [RESOLVED] Newtonsoft help
dday9 ... thank you for the suggestion. I will have to try that some time. In my particular case the JSON will always match the sample.