Wesley
Wesley

Reputation: 5621

Deserialize simple string array

I have a simple HTTPHandler pretending to be a Web Service (platform constraints, don't judge me)

I want to be able to create a string array in javascript, stringify it, and send it in a REQUEST header to be consumed as a set of parameters.

My issue is, most of the deserialization methods require you to create a named object and deserialize the whole object. I just want the simple string, man.

var ar = [];

ar.push("one");
ar.push("two");

var arStr = JSON.stringify(ar);

//$Ajax() bla bla bla
//sends out as "[\"one\",\"two\"]"

I'm sure there is a simple answer, but so far I can't find it.

Addition

Platform constraints also limit me from using 3rd part libraries. Needs to be straight .NET

Upvotes: 0

Views: 1273

Answers (2)

L.B
L.B

Reputation: 116168

Your json is a string array/List. All you need is (using Json.Net)

List<string> list = JsonConvert.DeserializeObject<List<string>>(jsonstring);

If you are using JavaScriptSerializer

var list = new JavaScriptSerializer().Deserialize<List<string>>(jsonstring);

BTW: if you are using ajax, you don't need to stringify the object. Just post it as object. The library internally handles it, otherwise you may need double-deserialiazation at receiver end.

Upvotes: 2

Alexander Gessler
Alexander Gessler

Reputation: 46637

String.join(",", ar) if you can make sure that your strings don't contain the separator character.

Upvotes: 0

Related Questions