将带有撇号的JSON值发送到Web服务时,对象错误无效 [英] Invalid object error when sending json values with apostrophe to webservice

查看:83
本文介绍了将带有撇号的JSON值发送到Web服务时,对象错误无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将json对象发送到Web服务,然后反序列化为自定义(LineItemWithDetails)对象.当在json的任何地方都存在apstrophes传递到Web服务时,我得到下面的错误.在这种情况下,它是预算合理化".我不知道该怎么办.

I am trying to send a json object to a webservice, to be deserialized into a custom (LineItemWithDetails) object. When there are apstrophes anywhere in the json being passed to the webservice, I get the error below. In this case it is "BudgetJustification". I do not know what I am supposed to do here.

$.ajax({
    type: "POST",
    url: baseUrl + "/WebService/BudgetGrid.asmx/SaveLineItemDetails",
    data: "{details: '" + JSON.stringify(_lineItemObj) + "', categoryId: " + _lineItemObj.LineItem.CategoryID + ", lineItemId: " + _lineItemObj.LineItem.ID + " }",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success:
        function (response) {
        },
    error:
        function (response) {
            ShowError(response.responseText);
        }
});



[WebMethod(true)]
    public string SaveLineItemDetails(string details, int categoryId, int lineItemId)
    {
        PersistantData session = PersistantData.getInstance();
        BudgetBase budgetBase = BudgetFactory.Retrieve((BudgetTypes)categoryId, session.GranteeID, session.GrantID, session.BudgetYear, session.BatchVersion, session.SourceID, session.ApplicationID, session.OriginID, session.BudgetChangeRequestFundingType, session.BatchID, session.UserID);
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        Gov.Grants.Budget.Business.LineItem.LineItemWithDetails d = serializer.Deserialize<Gov.Grants.Budget.Business.LineItem.LineItemWithDetails>(details);
        budgetBase.SaveDetails(d, lineItemId);
        return "";
    }

错误消息

"{"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. (428): {details: \u0027{\"LineItem\":{\"ID\":18494,\"ParentID\":18487,\"ApplicationID\":0,\"GranteeID\":57,\"BudgetYear\":2011,\"LineItemTypeID\":3,\"CategoryID\":3,\"CategoryName\":\"Travel\",\"VersionID\":0,\"OriginID\":2,\"AmtChange\":0,\"LineItemGuid\":\"fab8d064-767b-4526-8762-81a849361551\",\"Description\":\"PHIN conference - 1 + Trip(s) + [Out of State]\",\"ExceptionText\":\"Other: Out of state travel costs ($1,970) are restricted to NIC, Hepatitis B Coordinators\u0027 Meeting, Program Managers/PHA Meeting, ACIP meetings, VFC and AFIX training, and other CDC-sponsored immunization program meetings. \",\"DiffGram\":\"FA Travel Line Item Needs More Information.\",\"UserID\":452,\"AllocID\":57695,\"AllocGuid\":\"00000000-0000-0000-0000-000000000000\",\"IsReviewedLI\":true,\"NMIStatusID\":1,\"NMIStatus\":\"Not Addressed\"},\"Details\":{\"Location\":\"PHIN conference\",\"NumTrips\":1,\"NumPeople\":\"5\",\"NumDays\":5,\"AmtPerDi em\":56,\"NumNights\":4,\"AmtLodging\":150,\"AmtAir\":850,\"AmtOther\":240,\"NumMiles\":0,\"AmtMileage\":0.405,\"TravelTypeID\":2,\"TravelTypes\":[{\"LookupName\":\"TravelType\",\"Lookup_ID\":1,\"BudgetYear\":-1,\"LookupValue\":\"In State\",\"LookupKey\":\"\",\"LookupCode\":\"\",\"LookupText\":\"\"},{\"LookupName\":\"TravelType\",\"Lookup_ID\":2,\"BudgetYear\":-1,\"LookupValue\":\"Out of State\",\"LookupKey\":\"\",\"LookupCode\":\"\",\"LookupText\":\"\"}],\"DefaultNumOfMonth\":12,\"BudgetJustification\":\"This is the annual Public Health Information Network Conference. This is attended by the WVSIIS manager or designee to keep abreast with what\u0027s going on with PHIN, CRA and other public health applications and directions.\",\"Identifier\":\"PHIN\",\"CategoryConfig\":null}}\u0027, categoryId: 3, lineItemId: 18494 }","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.Deseria lizeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}"

Error Message

"{"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. (428): {details: \u0027{\"LineItem\":{\"ID\":18494,\"ParentID\":18487,\"ApplicationID\":0,\"GranteeID\":57,\"BudgetYear\":2011,\"LineItemTypeID\":3,\"CategoryID\":3,\"CategoryName\":\"Travel\",\"VersionID\":0,\"OriginID\":2,\"AmtChange\":0,\"LineItemGuid\":\"fab8d064-767b-4526-8762-81a849361551\",\"Description\":\"PHIN conference - 1 + Trip(s) + [Out of State]\",\"ExceptionText\":\"Other: Out of state travel costs ($1,970) are restricted to NIC, Hepatitis B Coordinators\u0027 Meeting, Program Managers/PHA Meeting, ACIP meetings, VFC and AFIX training, and other CDC-sponsored immunization program meetings. \",\"DiffGram\":\"FA Travel Line Item Needs More Information.\",\"UserID\":452,\"AllocID\":57695,\"AllocGuid\":\"00000000-0000-0000-0000-000000000000\",\"IsReviewedLI\":true,\"NMIStatusID\":1,\"NMIStatus\":\"Not Addressed\"},\"Details\":{\"Location\":\"PHIN conference\",\"NumTrips\":1,\"NumPeople\":\"5\",\"NumDays\":5,\"AmtPerDi em\":56,\"NumNights\":4,\"AmtLodging\":150,\"AmtAir\":850,\"AmtOther\":240,\"NumMiles\":0,\"AmtMileage\":0.405,\"TravelTypeID\":2,\"TravelTypes\":[{\"LookupName\":\"TravelType\",\"Lookup_ID\":1,\"BudgetYear\":-1,\"LookupValue\":\"In State\",\"LookupKey\":\"\",\"LookupCode\":\"\",\"LookupText\":\"\"},{\"LookupName\":\"TravelType\",\"Lookup_ID\":2,\"BudgetYear\":-1,\"LookupValue\":\"Out of State\",\"LookupKey\":\"\",\"LookupCode\":\"\",\"LookupText\":\"\"}],\"DefaultNumOfMonth\":12,\"BudgetJustification\":\"This is the annual Public Health Information Network Conference. This is attended by the WVSIIS manager or designee to keep abreast with what\u0027s going on with PHIN, CRA and other public health applications and directions.\",\"Identifier\":\"PHIN\",\"CategoryConfig\":null}}\u0027, categoryId: 3, lineItemId: 18494 }","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.Deseria lizeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}"

推荐答案

问题在于如何构造JSON.我根据您的代码创建了一个更简单的示例,该示例适用于我的测试应用程序(我使用的是VS2010/C#).在JSON中,您应该将属性名称和值都用引号引起来.这是我的示例应用程序的输出(大致基于您的代码):

The problem is with how your JSON is being constructed. I created a simpler example based on your code, and this works with my test app (I'm using VS2010/C#). In JSON, you should wrap both your property name and value in quotes. Here's the output from my sample app (which is loosely based on your code):

{
"details":"{\"Name\":\"David\",\"Age\":\"199\",\"FavColor\":\"Blue\",\"FavFood\":\"Pizza\",
\"boo\":{\"Foo\":\"hoo\",\"Bar\":\"ray\"}}",
"categoryId":1,
"lineItemId":2
}

因此,我的属性名称(即详细信息,categoryId,lineItemId)也用引号引起来.您的似乎并非如此,因此可能会导致JSON验证失败.

So my property names (i.e. details, categoryId, lineItemId) are wrapped in quotes, too. Yours don't seem to be -- so that could fail JSON validation.

最好使用JSON2.js文件将您的对象字符串化为JSON表示法.它是由Crockford(编写JSON规范)编写的,因此非常可靠.

You may be best using the JSON2.js file to stringify your objects into JSON notation. It was written by Crockford (who wrote the JSON spec), so it's pretty reliable.

这是我的JavaScript代码,用于对您的对象进行JSON修饰并进行调用:

Here's my JavaScript code to JSON-ify your object and make the call:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.3.min.js"></script>
<script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var value = { Name: 'David',
            Age: '199',
            FavColor: 'Blue',
            FavFood: 'Pizza',
            boo: { Foo: 'hoo',
                Bar: 'ray'
            }
        };

        var valJson = JSON.stringify(value);
        var dataJson = { details: valJson, categoryId: 1, lineItemId: 2 };
        var dataJsonString = JSON.stringify(dataJson);
        alert(dataJsonString);

        $.ajax({
            type: "POST",
            url: "BudgetGrid.asmx/SaveLineItemDetails",
            contentType: "application/json; charset=utf-8",
            data: dataJsonString,
            dataType: 'json',
            success: function (response) { alert('yay'); },
            error: function (response) { alert('boo'); }
        });

    });
</script>

这样就简化了BudgetGrid.asmx Web服务(我在代码中有一个像您一样的嵌套对象)....

And the BudgetGrid.asmx web service is simplified like this (I have a nested object like you do in your code)....

[Serializable]
class Stuff
{
    public string Foo { get; set; }
    public string Bar { get; set; }
}

[Serializable]
class Test1
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string FavColor { get; set; }
    public string FavFood { get; set; }
    public Stuff boo { get; set; }
}

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class BudgetGrid : System.Web.Services.WebService
{

    [WebMethod(true)]
    public string SaveLineItemDetails(string details, int categoryId, int lineItemId)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        Test1 boo1 = serializer.Deserialize<Test1>(details);

        return "";
    }
}

这篇关于将带有撇号的JSON值发送到Web服务时,对象错误无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆