【问题标题】:Reading JSON in C#在 C# 中读取 JSON
【发布时间】:2013-02-06 22:34:54
【问题描述】:

我正在尝试使用 Mozilla Persona 登录创建示例应用程序,但示例代码中出现错误。

代码

public class AuthController : Controller
{
    [HttpPost]
    public ActionResult Login(string assertion)
    {
        if (assertion == null)
        {
            // The 'assertion' key of the API wasn't POSTED. Redirect,
            // or whatever you'd like, to try again.
            return RedirectToAction("Index", "Home");
        }

        using (var web = new WebClient())
        {
            // Build the data we're going to POST.
            var data = new NameValueCollection();
            data["assertion"] = assertion;
            data["audience"] = "https://example.com:443"; // Use your website's URL here.


            // POST the data to the Persona provider (in this case Mozilla)
            var response = web.UploadValues("https://verifier.login.persona.org/verify", "POST", data);
            var buffer = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), Encoding.UTF8, response);


            // Convert the response to JSON.
            var tempString = Encoding.UTF8.GetString(buffer, 0, response.Length);
            var reader = new JsonReader();
            dynamic output = reader.Read(tempString);

            if (output.status == "okay")
            {
                string email = output.email; // Since this is dynamic, convert it to string.
                FormsAuthentication.SetAuthCookie(email, true);
                return RedirectToAction("Index", "Home");
            }

            // Could not log in, do something else.
            return RedirectToAction("Index", "Home");
        }
    }
}

错误

我在下面的行中收到一个错误,通知构造函数不能接受 0 个参数。好的,这很清楚。但是这段代码是我从Mozilla Persona 得到的。

var reader = new JsonReader();

更新

我在下面的代码中遇到了同样的错误

var reader = new JsonFx.Json.JsonReader();

有人可以帮助我吗?

我在stackoverflow中发现了一些问题,比如this one,你可以看到相同的代码。

【问题讨论】:

  • 阅读MSDN Docs on JSONReader。构造函数需要两个参数。
  • 是的,但他是我第一个将它与 C# 集成的应用程序,所以我按照 Mozilla 网站上的示例进行操作,但这行似乎是错误的。
  • 该示例使用 JsonFX JsonReader 并且它确实有一个默认构造函数。你得到的错误到底是什么。这是实际的 AuthController 源:github.com/sergiotapia/ASP.Net-MVC3-Persona-Demo/blob/master/…
  • 错误是:'JsonFx.Json.JsonReader'不包含接受0个参数的构造函数

标签: c# json json.net mozilla


【解决方案1】:

您需要升级到更新版本的 JsonFX,您可以在此处获取:https://github.com/jsonfx/jsonfx

在这个较新的版本中,JsonReader 实际上包含一个默认构造函数,它应该使您的代码能够工作。

在您可能拥有的版本中(我发现旧版本 here),JsonReader 有许多构造函数,但它们都不接受零参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多