I have a request parameter in my ASP.NET app. that is in JSON format, and I was wondering if there is a good (quick and easy) way to convert a JSON string to a Jayrocks JsonObject, so I can easily extract key-value pairs without the need to manually parse the string?
Convert JSON formated String to JsonObject with Jayrock
7.5k Views Asked by Marre At
2
There are 2 best solutions below
0

I don't know Jayrock, but if you want to accept a JSON object as a parameter of Action in MVC2 than the easiest way to do it is by using JsonValueProviderFactory from Futures assembly.
It's part of System.Web.Mvc in MVC3.
Assuming
json
is the variable containing JSON text, useJayrock.Json.Conversion.JsonConvert.Import(json)
. What you will get back in return is either aJsonObject
,JsonArray
,JsonNumber
,System.String
,System.Boolean
or a null reference depending on the root JSON value in the source JSON text. If you know it is going to be a JSON object for sure then you can safely cast the return value or useJsonConvert.Import<JsonObject>(json)
.I would discourage working against
JsonObject
directly unless you particularly depend on one of its features. You should just pretend the JSON object you get back is a dictionary; eitherIDictionary
orIDictionary<string, object>
. With the latest version for .NET Framework 4, you can also work with aJsonObject
as a dynamic object.