Note that there are some explanatory texts on larger screens.

plurals
  1. POJson adds \ charcter while returning json format
    primarykey
    data
    text
    <p>I am creating an API/web service which needs to return JSON format. I also need to create the web service as a POST request Sample code below, see more snippets of the source code in the end of this post.</p> <pre><code>Meta meta = new Meta(); meta.recipes = new List&lt;Recipe&gt;(); JavaScriptSerializer js = new JavaScriptSerializer(); string strJSON = js.Serialize(meta); return strJSON; </code></pre> <p>Problem: When I try the response in a few REST consoles (list of consoles tried) and in the ASP.NET client, I get this format with an extra "d" and extra \ before each ". See return output below:</p> <pre><code>{"d":"{\"count\":\"0\",\"status\":\"500\",\"recipes\":[]}"} </code></pre> <p>When I try to remove serialization then I get the following format:</p> <pre><code>&lt;Meta xmlns:xsi="w3.org/2001/XMLSchema-instance"; xmlns:xsd="w3.org/2001/XMLSchema"; xmlns="tempuri.org/"&gt;; &lt;count&gt;1&lt;/count&gt; &lt;status&gt;200&lt;/status&gt; &lt;recipes&gt; &lt;Recipe&gt; &lt;recipeID&gt;1&lt;/recipeID&gt; &lt;recipeName&gt;Apple Pie&lt;/recipeName&gt; &lt;imageURL&gt;service/it.jpg&lt;/imageURL&gt; &lt;rating/&gt; &lt;/Recipe&gt; &lt;/recipes&gt; &lt;/Meta&gt; </code></pre> <p>But I want it in the following format:</p> <pre><code>{"count":"0","status":"500","recipes":[]} [WebMethod(Description = "Return all Recipe...")] [ScriptMethod( ResponseFormat = ResponseFormat.Json)] public Meta RecipeList(string ingredientId, string cuisineId, string dishTypeId, string courseId) </code></pre> <p>This still returns XML even though I return meta object and don't add serialization</p> <p>Questions:</p> <ol> <li>I thought the correct JSON format should be WITHOUT this "d" and the . Is this true or is the correct JSON format of the output actually WITH the "d" and the \?</li> <li>If it should be without, then where do you suggest the correction should be made, on the server side or in the client side?</li> <li>How should I correct this on the server side?</li> <li><p>How can this be corrected on the client side?</p> <pre><code>[WebMethod(Description = "Return all Recipe...")] [ScriptMethod( ResponseFormat = ResponseFormat.Json)] public string RecipeList(string ingredientId, string cuisineId, string dishTypeId, string courseId, string occasionId, string considerationId, string recipeType, string readyTime, string favouritebyUserId, string bookmarkbyUserId) { DataSet ds = new DataSet(); int rTime = 0; if (readyTime == "") rTime = 0; else rTime = Convert.ToInt32(readyTime); ds = RecipeBLL.SearchRecipe(ingredientId, cuisineId, dishTypeId, courseId, occasionId, considerationId, recipeType, rTime); // Create a multidimensional jagged array string[][] JaggedArray = new string[ds.Tables[0].Rows.Count][]; int i = 0; Meta meta = new Meta(); int count = 0; meta.recipes = new List&lt;Recipe&gt;(); foreach (DataRow rs in ds.Tables[0].Rows) { Recipe recipe = new Recipe { recipeID = rs["RecipeId"].ToString(), recipeName = rs["RecipeTitle"].ToString(), imageURL = rs["Photo"].ToString(), rating = rs["Rating"].ToString() }; meta.recipes.Add(recipe); //mlist.Add(recipe); count++; } if (count != 0) meta.status = "200"; else meta.status = "500"; meta.count = count.ToString(); JavaScriptSerializer js = new JavaScriptSerializer(); string strJSON1 = js.Serialize(meta); return strJSON1; } </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload