简体   繁体   English

使用Jayrock将JSON格式化String转换为JsonObject

[英]Convert JSON formated String to JsonObject with Jayrock

I have a request parameter in my ASP.NET app. 我的ASP.NET应用程序中有一个请求参数。 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? 这是JSON格式,我想知道是否有一种好的(快速简便的)方法将JSON字符串转换为Jayrocks JsonObject,这样我就可以轻松提取键值对而无需手动解析字符串?

Assuming json is the variable containing JSON text, use Jayrock.Json.Conversion.JsonConvert.Import(json) . 假设json是包含JSON文本的变量,请使用Jayrock.Json.Conversion.JsonConvert.Import(json) What you will get back in return is either a JsonObject , JsonArray , JsonNumber , System.String , System.Boolean or a null reference depending on the root JSON value in the source JSON text. 您将获得的回报是JsonObjectJsonArrayJsonNumberSystem.StringSystem.Boolean或空引用,具体取决于源JSON文本中的根JSON值。 If you know it is going to be a JSON object for sure then you can safely cast the return value or use JsonConvert.Import<JsonObject>(json) . 如果你知道它肯定是一个JSON对象,那么你可以安全地转换返回值或使用JsonConvert.Import<JsonObject>(json)

I would discourage working against JsonObject directly unless you particularly depend on one of its features. 我不鼓励直接对抗JsonObject ,除非你特别依赖它的一个功能。 You should just pretend the JSON object you get back is a dictionary; 你应该假装你得到的JSON对象是一个字典; either IDictionary or IDictionary<string, object> . IDictionaryIDictionary<string, object> With the latest version for .NET Framework 4, you can also work with a JsonObject as a dynamic object. 使用.NET Framework 4的最新版本,您还可以将JsonObject用作动态对象。

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. 我不知道Jayrock,但是如果你想接受一个JSON对象作为MVC2中Action的参数,那么最简单的方法就是使用Futures程序集中的JsonValueProviderFactory

It's part of System.Web.Mvc in MVC3. 它是MVC3中System.Web.Mvc的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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