0

I write this code:

var filesNames = Directory.GetFiles(Server.MapPath("~/Image"))
                          .Select(x => Path.GetFileName(x));
var imgUrls = filesNames.Select(x => ResolveUrl(String.Format("~/Image/{0}",  x))).ToArray();

string[][] newKeys = imgUrls.Select(x => new string[] { x }).ToArray();
JavaScriptSerializer jss = new JavaScriptSerializer();

string json = jss.Serialize(newKeys);
Response.Write(json);

that code correctly convert imgUrl to json object and response result is:

[["/Image/t1.jpg"],["/Image/t2.jpg"],["/Image/t3.jpg"],["/Image/t4.jpg"]]

How can i add "url" fields to my json object?

for example i want convert to this:

[["url:","/Image/t1.jpg"],["url:","/Image/t2.jpg"],["url:","/Image/t3.jpg"],["url:","/Image/t4.jpg"]]

1 Answer 1

3

You could map the imgUrls into anonymous objects instead of string arrays:

var newKeys = imgUrls.Select(x => new { url = x }).ToArray();

JavaScriptSerializer jss = new JavaScriptSerializer();
string json = jss.Serialize(newKeys);
Response.Write(json);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.