Json在返回json格式时添加了\ charcter

时间:2012-12-25 12:42:59

标签: asp.net json

我正在创建一个需要返回JSON格式的API / Web服务。 我还需要将Web服务创建为POST请求 下面的示例代码,请参阅本文末尾的更多源代码片段。

Meta meta = new Meta(); 
meta.recipes = new List<Recipe>(); 
JavaScriptSerializer js = new JavaScriptSerializer(); 
string strJSON = js.Serialize(meta); 
return strJSON;

问题: 当我在几个REST控制台(尝试的控制台列表)和ASP.NET客户端中尝试响应时,我得到这个格式带有额外的“d”和额外的\在每个“之前”。请参阅下面的返回输出:

{"d":"{\"count\":\"0\",\"status\":\"500\",\"recipes\":[]}"}

当我尝试删除序列化时,我得到以下格式:

<Meta xmlns:xsi="w3.org/2001/XMLSchema-instance"; xmlns:xsd="w3.org/2001/XMLSchema"; xmlns="tempuri.org/">; <count>1</count> <status>200</status> <recipes> <Recipe> <recipeID>1</recipeID> <recipeName>Apple Pie</recipeName> <imageURL>service/it.jpg</imageURL> <rating/> </Recipe> </recipes> </Meta> 

但我希望它采用以下格式:

{"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)

即使我返回元对象并且不添加序列化

,它仍会返回XML

问题:

  1. 我认为正确的JSON格式应该没有这个“d”和。这是真的还是输出的正确JSON格式实际上是“d”和\?
  2. 如果没有,那么您应该在服务器端或客户端建议进行更正?
  3. 我应该如何在服务器端更正此问题?
  4. 如何在客户端进行纠正?

    [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<Recipe>();
        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;
    }
    

1 个答案:

答案 0 :(得分:1)

听起来问题是从某个地方的代码中返回一个字符串 - 然后它被其他东西编码为JSON。所以你要返回的字符串是:

{"count":"0","status":"500","recipes":[]}

...但是无论你回来的是什么,都认为你尝试来返回一个字符串,而不是一个带有计数等的对象。

您没有显示任何代码,但我怀疑答案是删除一个显式序列化调用。