Json does not contain a definiton for 'stringi

2019-09-22 08:15发布

问题:

I am trying to update a JIRA issue using Visual Studio 2015, Microsoft Razor, and C# with Json.Net 10.0.2. The code is:

public String updateJiraIssue(object objJira2) {
    JiraService.open("PUT", JiraUrl + "/rest/api/2/issue/NPI-24");
    JiraService.setRequestHeader("Content-Type", "application/json");
    JiraService.setRequestHeader("Accept", "application/json");
    JiraService.setRequestHeader("X-Atlassian-Token", "nocheck");
    JiraService.setRequestHeader("Authorization", "Basic " + GetEncodedCredentials());
    var myJSON = Json.stringify(JiraJson);
    JiraService.send(myJSON);
    String response = JiraService.responseText;
    JiraService.abort();
    return response;
}

The error occurs in:

var myJSON = Json.stringify(JiraJson);

The JSON is:

string jsonString = @"{""fields"":{""customfield_12302"":{""name"":""xyz""}}}";

回答1:

JSON.stringify() is a javascript function, which you would call from a web page script to serialize an object to JSON before making an ajax request.

In C#, there is a Json class in the System.Web.Helpers namespace, but its serialization method is called Encode, not stringify. Note this class uses the JavaScriptSerializer internally to do its work; it does not depend on Json.Net.

If you want to use Json.Net, you should be calling JsonConvert.SerializeObject() instead:

var myJSON = JsonConvert.SerializeObject(objJira2);