Convert JSON-formatted string to JsonObject with Jayrock

I have a query parameter in my ASP.NET application. that is, in JSON format, and I was wondering if there is a good (quick and easy) way to convert a JSON string to Jayrocks JsonObject, so I can easily extract key-value pairs without having to manually parse the string?

+4
source share
2 answers

Assuming json is a variable containing JSON text, use Jayrock.Json.Conversion.JsonConvert.Import(json) . That you will return in return is either JsonObject , JsonArray , JsonNumber , System.String , System.Boolean , or a null reference depending on the JSON value in the JSON source text. If you know that this will be a JSON object, then you can safely discard the return value or use JsonConvert.Import<JsonObject>(json) .

I would refuse to work with JsonObject directly if you do not depend on only one of its functions. You should just pretend that the JSON object you are returning is a dictionary; either IDictionary or IDictionary<string, object> . With the latest version for the .NET Framework 4, you can also work with JsonObject as a dynamic object.

+6
source

I don't know Jayrock, but if you want to accept a JSON object as an Action parameter in MVC2, and the easiest way to do this is to use the JsonValueProviderFactory from the futures assembly.

This is part of System.Web.Mvc in MVC3.

0
source

Source: https://habr.com/ru/post/1336459/


All Articles