Lucas_Santos
Lucas_Santos

Reputation: 4740

Reading JSON in C#

I'm trying do a sample application with Mozilla Persona login, but I got an error in a sample code.

CODE

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");
        }
    }
}

ERROR

I got a error in the line below, that inform that the constructor cannot take 0 arguments. OK, this is very clear. But this code I got from Mozilla Persona.

var reader = new JsonReader();

UPDATE

I got the same error with the code below

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

Someone can help me ?

I found some question in stackoverflow, like this one that you can see the same piece of code.

Upvotes: 1

Views: 2035

Answers (1)

nick_w
nick_w

Reputation: 14938

You need to upgrade to a more recent version of JsonFX, which you can get here: https://github.com/jsonfx/jsonfx.

In this more recent version, JsonReader does in fact contain a default constructor, which should enable your code to work.

In the version you probably have (I found the older version here), JsonReader has a number of constructors but none of them accept zero arguments.

Upvotes: 2

Related Questions