5

I am updating an old classic ASP site to a new .net 3.5 version. The page has a custom list control which the client (my boss) wants to keep. This list control requires several arrays in order to work correctly. the array is a multi-dimensional list of publications. This is what it looks like:

var publicationTable = [
 [31422,"Abilene Reporter News","Abilene","TX",false,"D",0],
 [313844,"Acadiana Weekly","Opelousas","LA",false,"W",1],
 [527825,"Action Advertiser","Fond du Lac","WI",false,"W",2]...n]

I want to generate this array server side and register it. I have looked at the msdn but this is a little trivial. The conceptual issue is that the array is a mixture of string and ints and I am not sure how to recreate this, so how?

Erick Petrucelli
15.1k9 gold badges68 silver badges89 bronze badges
asked May 7, 2011 at 0:25
2
  • 1
    You need a JavaScript var with an array exactly like your C# array? Commented May 7, 2011 at 0:35
  • 4
    I would recommend you don't inject data from the server into JavaScript directly like this. Either get this data through some kind of ajax call or inject it straight into the HTML and progressively enhance the hTML with javascript. Commented May 7, 2011 at 0:37

3 Answers 3

16

You should do this:

Code behind:

using System.Web.Script.Serialization;
...
public string getJson(){
 var publicationTable = new List<object>{
 new []{ 31422,"Abilene Reporter News","Abilene","TX",false,"D",0},
 new []{ 313844,"Acadiana Weekly","Opelousas","LA",false,"W",1 },
 new []{ 527825,"Action Advertiser","Fond du Lac","WI",false,"W",2}
 };
 return (new JavaScriptSerializer()).Serialize(publicationTable);
}

Ase you see, to create an array of mixed types, we create an array of anonymous type with new []. You could also have done it with new object[].

Aspx file:

<script>
 var publicationTable = <%= getJson() %>;
</script>

Hope this helps. Cheers

answered May 7, 2011 at 0:46

2 Comments

Best answer given the poster clearly has no intention of implementing any sort of rest/json service.
Combining this with Linq has made it sooo much easier to do this in my MVC views when needed! Thanks!
1

I think a List<List<object>> containing your items, passed through the JavaScriptSerializer would do the trick. Given that this data probably comes from a more structured data type, you could probably do better than List<List<object>>, but the JavaScriptSerializer is probably what you're after.

answered May 7, 2011 at 0:38

Comments

0

Will this work?

ArrayList list = new ArrayList{31422,"Abilene Reporter News","Abilene","TX",false,"D",0};
var str = string.Format("[{0}]", list.Cast<object>().Select(x => (x is string)?
 string.Format(@"""{0}""", x) : x.ToString())
 .Aggregate((x, y) => string.Format("{0}, {1}", x, y)));

it can be extended for a multi-dimensional array in a similar fashion.

answered May 7, 2011 at 0:38

1 Comment

Could get messy if the strings contain backslashes or quotes or similar. Would really require a Javascript escaping layer to make it bulletproof.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.