Invalid JSON when parsing objects containing html as a string property - c#

I've got a controller action that just displays a JSON result: http://pkssblog-stage.azurewebsites.net/BlogPosts/GetAll. The object has a property called Content which is basically the HTML content of a post.
The problem is I always get invalid JSON exceptions when parsing it using the JsonObject.Parse in WinRT, the JSON.parse within a browser console or even JSON validators online.
The weird thing is that I don't get issues when I turn it into a javascript object via the console through var x = jsonstringarray;
I get in trouble with this specific property (as noted by the online validators):
"Content":"\u003cbr\u003eMicrosoft Azure Storage provides many services...
Any ideas?

Related

How to get http post request body without property in c#

I am creating an API. Where I have to accept JSON data. I found the JSON not properly fit with my property class. In this scenario how should I get data from post request?
I have tried with WEB API but it's showing null error
System.NullReferenceException: 'Object reference not set to an instance of an object.'
You cannot always change the class like #GuruStron suggests. Because it could have dependencies on other parts of the program.
What you should do : Customize your JSON parser
You probably already use Newtonsoft to parse your incoming body request into your class. Now what you need is to customize that parser so it fit your need.
Here is the doc: How to create a custom JsonCoverter
If you don't use Newtonsoft you probably use default System.TextJson in that case here are the docs
No matter which "parser" you use, you'll probably find an equivalent on the web
Please provide more details (and some code) for a more precise answer.
I don't know if you are receiving the json via an HttpClient, but in the case you can use ExpandoObject or dynamic as read target from the content.
es.
var response = await httpClient.GetAsync(url);
var result = await response.Content.ReadFromJsonAsync<dynamic>();
or
var result = await response.Content.ReadFromJsonAsync<ExpandoObject>();
In any case, case the deserialization to dynamic or ExpandoObject could work also from a string (see this).

How to serialize an object in MVC 5 Razor view

I have tried to follow this post in order to create a 3 page form wizard that passes data to each page.
He uses the HTML helper serialize, to serialize an object in the view.
#Html.Serialize("wizard", Model)
However this HTML helper isn't available in MVC 5 it seems.
I found another related post to this here where he suggests using the following to serialize the object.
#Html.Hidden("otherComplexData", new Microsoft.Web.Mvc.MvcSerializer().Serialize(complexObject))
But I then get the following error
There is no argument given that corresponds to the required formal parameter 'mode' of 'MvcSerializer.Serialize(object, SerializationMode)'
It seems to want a SerializationMode, however the documented one doesn't. https://msdn.microsoft.com/en-us/library/microsoft.web.mvc.mvcserializer.serialize(v=vs.118).aspx
What direction can I go in now?
Thanks.
Here's the Serialization option you need:
https://github.com/ASP-NET-MVC/ASP.NET-Mvc-3/blob/master/mvc3/src/MvcFutures/Mvc/SerializationMode.cs
Options are Signed or EncryptedAndSigned.
You can try that and see if it will work.
There's multiple ways to encode data that will work for you. You could put the values in a hidden input using Json.Encode for the view, and Json.Decode on the server side.

How to generate angularjs model from c# class

The angular app I am working on has several large input forms which span several pages. The data is added to an angular model which is a javascript object literal and sent to a webApi controller. The webApi parameter for my POST methods is a C# class (duh) with a lot of properties! Is there a utility which will generate the javascript from my C# class, so that my binding just works! I've googled this and failed even though it seems such a mundane task. As always thanks in advance.
You should use JSON serialize on your JS side and deserialize on your c# side.
JS:
System.Web.Script.Serialization.JavaScriptSerializer serializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
string jsonParam = oSerializer.Serialize(param);
While on your c# side you should use something like, lets say your class is Person:
C#:
Person person = new JavaScriptSerializer().Deserialize<Person>(param);
see msdn documentation on serialize\deserialize json object

XML Deserialization with "mixed" type

I am trying to consume XML from a web service. I initially started using LINQ to XML but after learning about serialization/deserialization I decided I would instead just try to deserialize the XML.
Although I haven't yet written the deserialization code, I've realised that due to how the web service works, I can't always know the actual type being returned for certain elements.
For example consider this XML (http://api.wunderground.com/api/d36f54198ebbb48c/conditions/q/England/London.xml)
<dewpoint_string>52 F (11 C)</dewpoint_string>
<dewpoint_f>52</dewpoint_f>
<dewpoint_c>11</dewpoint_c>
<heat_index_string>NA</heat_index_string>
<heat_index_f>NA</heat_index_f>
<heat_index_c>NA</heat_index_c>
Now, normally provided that all the data is available then elements such as "heat_index_f" would be a decimal/double. However when certain parts of the data is not available the service will instead have the string value of "NA" in the element. I am deserializing to an object, that has properties for each element. EG: string HeatIndexString; double HeatIndexF; Obviously, if the service returns with the string NA then I will get a runtime exception of trying to convert "NA" to a double.
How should I tackle this problem? Is there some type of attribute I can tell the deserializer to somehow work with these "mixed" types? How do other developers deal with situations like this?

.net web service

i am calling a web service by aspx page and wants my return output as a json format with a specific view like (grid view) in asp.net...so here is any view available in JSON which display my output like grid view..pls give me suggetion as soon as possible....thax
salman ansari
As far as I know, WebServices are based on XML SOAP objects, I anot sure if these can be handled like Json objects or converted. You could create a page (or view in MVC), that returns json objects specifically.

Categories