makkesk8
makkesk8

Reputation: 45

Deserialize json passed by javascript to c#

Im currently trying to deserialize json that got bassed by javascript this way:

window.external.handlemessage(json);

And its being handled by c# like this:

   public void handlemessage(string json)
   {
            JavaScriptSerializer deserializer = new JavaScriptSerializer();

            Dictionary<string, object> deserializedDictionary1 = (Dictionary<string, object>)deserializer.Deserialize(json, typeof(object));
            Dictionary<string, object> deserializedDictionary2 = deserializer.Deserialize<Dictionary<string, object>>(json);
            object objDeserialized = deserializer.DeserializeObject(json);

   }

The passing works fine with plain text for example but just not with json..

I've tried several things such as the deserialize example i provided in the handlemessage but the json just returns invalid basicly. And several other examples ive tried just didn`t do it either.

I have tried to deserialized the json with java and serialized it again without no results (incase there were some flaw).

Also Im trying to deserialize the data without knowing the json structure.

Is it even possible to pass json by javascript and unserializing it with c#?

Upvotes: 0

Views: 1025

Answers (1)

Joey Gennari
Joey Gennari

Reputation: 2361

Also Im trying to deserialize the data without knowing the json structure.

For that you want to use C#'s dynamic type:

JavaScriptSerializer js = new JavaScriptSerializer();
dynamic v = js.Deserialize<dynamic>("{\"text\" : \"hi\"}");

In fact you can start there for all your data until you understand how the object is being mapped.

Upvotes: 2

Related Questions