WCF DeSerialize form-urlencode数据到类对象

时间:2017-03-29 06:30:26

标签: asp.net wcf

我想反序列化x-www-form-urlencoded string

的对象
public void TestData1(Stream data)
    {
        StreamReader sr = new StreamReader(data);
        string strJson = sr.ReadToEnd();

        var response = Newtonsoft.Json.JsonConvert.DeserializeObject<Recipe>strJson);
    }

以下是我的申请/ x-www-form-urlencoded

title=test&recipe_accessories=%5B+++++%7B+++++%22accessories_id%22%3A%22ingr001%22+++++++%7D%2C++++++%7B+++++%22ingredient_id%22%3A%22ingr001%22+++++%7D%2C++++++%7B+++++%22ingredient_id%22%3A%22ingr001%22+++++%7D+++%5D&recipe_ingredient=%5B+++++%7B+++++%22ingredient_id%22%3A%22ingr001%22%2C+++++%22ingredient_qty_num%22%3A%222.5%22%2C+++++%22ingredient_qty_unit%22%3A%22cup%22++++++++++++%7D%2C++++++%7B+++++%22ingredient_id%22%3A%22ingr001%22%2C+++++%22ingredient_qty_num%22%3A%222.5%22%2C+++++%22ingredient_qty_unit%22%3A%22cup%22++++++++++++%7D%2C++++++%7B+++++%22ingredient_id%22%3A%22ingr001%22%2C+++++%22ingredient_qty_num%22%3A%222.5%22%2C+++++%22ingredient_qty_unit%22%3A%22cup%22++++++++++++%7D+++%5D&recipe_img=%5B+++++%22http%3A%2F%2Fcontrol%2Fimages%2Ftest.jpg%22%2C+++++%22http%3A%2F%2Fcontrol%2Fimages%2Ftest.jpg%22%2C+++++%22http%3A%2F%2Fcontrol%2Fimages%2Ftest.jpg%22%2C+++++%22http%3A%2F%2Fcontrol%2Fimages%2Ftest.jpg%22%2C+++++%22http%3A%2F%2Fcontrol%2Fimages%2Ftest.jpg%22+++%5D

错误

Error parsing boolean value. Path '', line 1, position 1.'. See server logs for more details. The exception stack trace is: at Json.JsonTextReader.ParseTrue()

原始格式的实际Json

{
  "recipe_img": [
    "http://control/images/test.jpg",
    "http://control/images/test.jpg",
    "http://control/images/test.jpg",
    "http://control/images/test.jpg",
    "http://control/images/test.jpg"
  ],
  "recipe_ingredient": [
    {
    "ingredient_id":"ingr001",
    "ingredient_qty_num":"2.5",
    "ingredient_qty_unit":"cup"

    },
     {
    "ingredient_id":"ingr001",
    "ingredient_qty_num":"2.5",
    "ingredient_qty_unit":"cup"

    },
     {
    "ingredient_id":"ingr001",
    "ingredient_qty_num":"2.5",
    "ingredient_qty_unit":"cup"

    }
  ],
  "recipe_accessories": [
    {
    "accessories_id":"ingr001"
      },
     {
    "ingredient_id":"ingr001"
    },
     {
    "ingredient_id":"ingr001"
    }
  ],
  "title": "Hello WOrld",  
}

当我使用 BodyStyle = WebMessageBodyStyle.Bare 并以RAW格式传递Json时,它可以正常工作。

BodyStyle = WebMessageBodyStyle.WrappedRequest

时无效

这是我的班级

public class Recipe
{
    public IList<string> recipe_img { get; set; }
    public IList<RecipeIngredient> recipe_ingredient { get; set; }
    public IList<RecipeAccessory> recipe_accessories { get; set; }
    public string title { get; set; }
}


public class RecipeIngredient
{
    public string ingredient_id { get; set; }
    public string ingredient_qty_num { get; set; }
    public string ingredient_qty_unit { get; set; }
}

public class RecipeAccessory
{
    public string accessories_id { get; set; }
    public string ingredient_id { get; set; }
}

0 个答案:

没有答案