1

I have a simple array that looks like this:

{"image_01.jpg","image_02.jpg","image_03.jpg"}

I need to format it into this:

[{"small":"image_01.jpg","big":"image_01.jpg"},{"small":"image_02.jpg","big":"image_02.jpg"}, {"small":"image_03.jpg","big":"image_03.jpg"}]

I am very new to ASP.NET C#, I've been trying the examples here: https://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx but I am not sure how to implement them into my array.

4
  • 2
    it's not exactly a multidimensional array, it looks more like an array of objects created from an array of strings Commented Dec 29, 2016 at 10:49
  • Thanks, I updated the title of my question. Commented Dec 29, 2016 at 10:54
  • Have a look here. Commented Dec 29, 2016 at 10:54
  • Is the first array a C# array? If so then it should be written as { "image_01.jpg", "image_02.jpg", "image_03.jpg" } or even new[] { "image_01.jpg", "image_02.jpg", "image_03.jpg" }. Commented Dec 29, 2016 at 11:05

1 Answer 1

3

you need to import the System.Linq and System.Web.Script.Serialization namespaces and then use the following code:

var array = new string[] { "image_01.jpg", "image_02.jpg", "image_03.jpg" };
var newArray = array.Select(x => new { small = x, big = x }).ToArray();

var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(newArray);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.