【问题标题】:Cannot convert object of type 'System.String' to type 'PropertySearch'无法将“System.String”类型的对象转换为“Property Search”类型
【发布时间】:2013-06-12 06:17:15
【问题描述】:

我有一个 weservice 方法,

public string SearchProperties(string pPropertySearch, string pSort)
    {
        string[] _propSearch = pPropertySearch.Split(',');
        string searchType = (_propSearch)[_propSearch.Length - 1];
        List<Property> returnResults = new List<Property>();
        try
        {
            PropertySearch _MyPropertySearch = (PropertySearch)JavaScriptConvert.DeserializeObject(pPropertySearch, typeof(PropertySearch));
            returnResults = PropertyDALC.SearchPropertyNew(_MyPropertySearch, pSort, searchType, 25);
        }
        catch (Exception e)
        {

        }
        string output = JavaScriptConvert.SerializeObject(returnResults);

        return output;
    }

PropertySearch.cs,

[Serializable]
public class PropertySearch
{
private string userId;
private string sessionId;

private string priceMin;
private string priceMax;


public string UserId
{
    get { return userId; }
    set { userId = value; }
}
public string SessionId
{
    get { return sessionId; }
    set { sessionId = value; }
}

public string PriceMin
{
    get { return priceMin; }
    set { priceMin = value; }
}
public string PriceMax
{
    get { return priceMax; }
    set { priceMax = value; }
}
public string BedsMin
{
    get { return bedsMin; }
    set { bedsMin = value; }
}         
}

我将字符串“pPropertySearch”作为:

 "0000-0000","gghuk","6666","8888"

当点击 InVoke 按钮时,webservice 被调用并在这一行附近

" PropertySearch _MyPropertySearch = (PropertySearch)JavaScriptConvert.DeserializeObject(pPropertySearch, typeof(PropertySearch));"

它给出了这个例外,

 "Cannot convert object of type 'System.String' to type 'PropertySearch'"

这段代码有什么问题。请纠正我。

我是否以错误的格式传递字符串?请帮忙。 webservice中的这些序列化、反序列化概念我不知道。

感谢任何帮助!

【问题讨论】:

    标签: .net json web-services deserialization


    【解决方案1】:

    您似乎正在尝试使用无效的 json 进行反序列化。检查 json 格式的好地方是JsonLint.com。你会希望你的 json 对象看起来更像这样:

    {
        "UserId": 1,
        "SessionId": 1,
        "PriceMin": 1,
        "PriceMax": 1,
        "BedsMin": 1
    }
    

    您遇到的问题是您传递的是逗号分隔的字符串。反序列化器不知道将哪些值映射到哪些字段。

    【讨论】:

    • @DSlangle,非常感谢!!
    猜你喜欢
    • 2012-09-07
    • 2014-03-14
    • 2017-11-24
    • 1970-01-01
    • 2021-04-26
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多