views:

47

answers:

0

This is a little embarrassing question but here it goes.

I have a hidden asp.net field that gets populated with a json string just before post back. I use JSON.stringify to set the value of the field like this

function prepareForSave() {
                var commissionSplits = new Array;
                $("input[id$='ctlBidAgentId']").each(function() {
                    debugger;
                    var obj = new Object;
                    var bidAgentId = $(this).val();
                    var percent = $(this).next("input[id$='ctlPercent']").val();

                    obj.BidAgentId = bidAgentId;
                    obj.CommissionPercent = percent;

                    commissionSplits.push(obj);
                });


                $("#<%=ctlJsonCommissionSplits.ClientID%>").val(JSON.stringify(commissionSplits));
            }

My problem is that when value gets assigned to the hidden field, all double qoutes get escaped.

Here is what stringify does:

?JSON.stringify(commissionSplits)
"[{"BidAgentId":"35454","CommissionPercent":"20"},{"BidAgentId":"35455","CommissionPercent":"30"}]"

and here is what the field value is after assignment

?ctlJsonCommissionSplits.Value
"[{\"BidAgentId\":\"35454\",\"CommissionPercent\":\"20\"},{\"BidAgentId\":\"35455\",\"CommissionPercent\":\"30\"}]"

On the server side I do this

/

/same result as deserialize
//var commissionsPerAgent = JArray.Parse( HttpUtility.HtmlDecode(ctlJsonCommissionSplits.Value));
       var commissionsPerAgent = (JArray)JsonConvert.DeserializeObject(ctlJsonCommissionSplits.Value);

                    var q = from o in commissionsPerAgent.Children<JObject>()
                            select new KeyValuePair<string, string>(
                                                    o.SelectToken("BidAgentId").ToString(),
                                o.SelectToken("CommissionPercent").ToString());
                                //Convert.ToInt32(o.SelectToken("BidAgentId").ToString()), 
                            //Convert.ToDouble(o.SelectToken("CommissionPercent").ToString()));

and I get this

?commissionsPerAgent[0].ToString()
"{\r\n  \"BidAgentId\": \"35454\",\r\n  \"CommissionPercent\": \"10\"\r\n}"
?q.ElementAt(0)
{System.Collections.Generic.KeyValuePair<string,string>}
    Key: Implicit function evaluation is turned off by user.
    key: "\"35454\""
    Value: Implicit function evaluation is turned off by user.
    value: "\"10\""

So the embarrassing question is how to get rid of these escape characters?