title: asp.net mvc json.net response
body: <p>I am implementing, for the first time, Json.NET for my ASP.NET MVC2 site.</p>
  
  <p>My original code looked like this:</p>
  
  <pre><code>[HttpPost]
  public ActionResult FindMe(string searchFirstName, string searchLastName)
  {
      this.searchFirstName = searchFirstName;
      this.searchLastName = searchLastName;
      IEnumerable&lt;HomePageUser&gt; results = doSearch();
      bool success = (results.Count() == 0) ? false : true;
  
      return Json(new
      {
          success = success,
          results = results
      });
  }
  </code></pre>
  
  <p>The results were problematic due to the fact that one of the items in the results set is an Enum and I really want the Text value, not the numeric one.  Additionally, the date format is a problem.  </p>
  
  <p>So, I then found Json.NET and changed my code to this:</p>
  
  <pre><code>[HttpPost]
  public JsonNetResult FindMe(string searchFirstName, string searchLastName)
  {
      this.searchFirstName = searchFirstName;
      this.searchLastName = searchLastName;
      IEnumerable&lt;HomePageUser&gt; results = doSearch();
      bool success = (results.Count() == 0) ? false : true;
  
      JsonNetResult jsonNetResult = new JsonNetResult();
      jsonNetResult.SerializerSettings.Converters.Add(new IsoDateTimeConverter());
      jsonNetResult.Data = results;// how to add success true/false info?
  
      return jsonNetResult;
  }
  </code></pre>
  
  <p>This fixes the above two issues, but now I'm wondering how to make this seamless with my existing javascript code that was expecting json that looked like:</p>
  
  <pre><code>{
      "success":true,
      "results":[{
          "UserId":545,
          "FirstName":"Scott",
          "LastName":"Roberson"}]
  }
  </code></pre>
  
  <p>This allowed me to first test for response.success before proceeding to write out the answer, versus moving into the section to handle errors.</p>
  
  <p>So, my question is how do I add a top-level success json node to live beside the results node?</p>
  
  <p>Thanks.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>As is often the case, the act of writing out a question sparked an idea in one of those Doh! moments.</p>
  
  <p>if I add:</p>
  
  <pre><code>var returnPackage = new { success = success, results = results};
  </code></pre>
  
  <p>then add this to the jsonNetResult.Data like so:  </p>
  
  <pre><code>jsonNetResult.Data = returnPackage;
  </code></pre>
  
  <p>It works perfectly.</p>
  
  <p>Thanks, anyway.</p>
  
  <p>Final code:</p>
  
  <pre><code>[HttpPost]
  public JsonNetResult FindMe(string searchFirstName, string searchLastName)
  {
      this.searchFirstName = searchFirstName;
      this.searchLastName = searchLastName;
      IEnumerable&lt;HomePageUser&gt; results = doSearch();
      bool success = (results.Count() == 0) ? false : true;
  
      var returnPackage = new { success = success, results = results};
  
      JsonNetResult jsonNetResult = new JsonNetResult();
      jsonNetResult.SerializerSettings.Converters.Add(new IsoDateTimeConverter());
      jsonNetResult.Data = returnPackage;
  
      return jsonNetResult;
  }
  </code></pre>