简体   繁体   中英

Deserialize JSON string with & using JSON.NET?

I have a JSON string stored in a cookie that has a & character in it:

{"Description":"The Livewire TS+ unlocks your vehicle's hidden performance by re-calibrating your vehicle's computer for Maximum Horsepower & Torque, Increased Throttle Response, Firmer Shifts and even Increased Fuel Mileage.","Sku":"SCTLWTSP","Count":1}

When I try to deserialize it, it throws an error about an illegal character. I have narrowed this down to the & character. I got around this by doing .Replace("&","%26") , but this stores the value back in the cookie with %26 .

What is the best way to handle the & or any other character that might be a problem? I am trying to keep the original string (not encoded) in the cookie.

I have tried Html.Encode , but this causes more illegal character problems when deserializing and I have tried Uri.EscapeDataString , but this did nothing.

To put some context this:

When the user clicks add to cart , it adds a serialized object to a cookie . When they click add to cart again, I deserialize the JSON string so I can update the Count variable, but this is when the error happens because of the & character?

I have a custom class ShoppingCartItem , I am storing an array of ShoppingCartItem in the cookie. Here is some code:

This is when I want to read the cookie string back into a List<ShoppingCartItem>

List<ShoppingCartItem> shoppingCartItems = JsonConvert.DeserializeObject<List<ShoppingCartItem>>(shoppingCartCookie["ShoppingCartItems"]);

When I write to the cookie, I use:

shoppingCartCookie["ShoppingCartItems"] = JsonConvert.SerializeObject(shoppingCartItems);

Json array example:

ShoppingCartItems=[{"Name":"X4 Performance Programmer ","ShortDescription":"The X4 Power Flash arrives Pre-Loaded with DYNO Proven tune files that INCREASE HORSEPOWER and TORQUE! ","Sku":"SCTX4","Count":1},{"Name":"Livewire TS Plus","ShortDescription":"The Livewire TS+ unlocks your vehicle's hidden performance by re-calibrating your vehicle's computer for Maximum Horsepower & Torque, Increased Throttle Response, Firmer Shifts and even Increased Fuel Mileage.","Sku":"SCTLWTSP","Count":1}]

Here is the exact line and error:

The error happens when this line is executed:

shoppingCartItems = JsonConvert.DeserializeObject<List<ShoppingCartItem>>(shoppingCartCookie["ShoppingCartItems"]);

The error is:

Unterminated string. Expected delimiter: ". Path '[1].ShortDescription', line 1, position 359.

I think, most likely, your issue isn't directly related to json serialization, but it is related to the fact that & isn't a valid character to put in a cookie (see this answer ).

You will have to use HttpUtility.UrlEncode and HttpUtility.UrlDecode . Eg:

var json = JsonConvert.SerializeObject(myList);
var cookieString = HttpUtility.UrlEncode(json);
// write cookie
...
// later when reading the cookie
var cookieString = ...
var json = HttpUtility.UrlDecode(cookieString);
var myList = JsonConvert.DeserializeObject<List<ShoppingCartItem>>(json); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM