-2

I have a Web Api 2 end point that returns Json containing an array of image urls. The response from it looks like this

   {
    "Status": 0,
    "Message": "Success",
    "Images": [
        "http://some.image.com/somepath/SomeReport-0.png",
        "http://some.image.com/somepath/SomeReport-1.png"
    ]
   }

I would like the json array to return as an array string. How would I need to set up my response in order to accomplish that? I currently set up the array and the response using this method

// response.status and response.message are added before this

response.Images = imagePaths.Select(x => string.Format(urlString, Path.GetFileName(x)));

HttpResponseMessage apiResponse = Request.CreateResponse(HttpStatusCode.OK, response);

purpose for needing the array returned in string format is so I can appended it to an html data-attribute. Right now if I reference the response like so:

listItem.append('<a class="dd-option" data-path="' + response.Images + '" href="#"></a>');

The content inside the attribute looks like

data-attr="http://some.url.com/somepath/ReportStore/image-0.png,http://some.url.com/somepath/ReportStore/image-1.png"

rather than

data-attr="["http://some.url.com/somepath/ReportStore/image-0.png","http://some.url.com/somepath/ReportStore/image-1.png"]"
Stavros_S
  • 2,145
  • 7
  • 31
  • 75
  • possible duplicate of [Deserializing JSON into string array](http://stackoverflow.com/questions/14640028/deserializing-json-into-string-array) – MethodMan Apr 07 '15 at 21:41
  • How about just using the javascript join() function? `response.Images.join(", ")`. – Jasen Apr 07 '15 at 21:46
  • hmm that's an interesting idea, I will give that a shot. – Stavros_S Apr 07 '15 at 21:48
  • @Jasen Unfortunately that did not work, I need the array returned as a string itself. – Stavros_S Apr 08 '15 at 00:36
  • It is possible in c# as well https://msdn.microsoft.com/en-us/library/57a79xd0(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2 – Jasen Apr 08 '15 at 00:42

2 Answers2

1

If you're doing this in javascript then you can JSON.stringify

var jsonResponse = 
    {
    "Status": 0,
    "Message": "Success",
    "Images": [
        "http://some.image.com/somepath/SomeReport-0.png",
        "http://some.image.com/somepath/SomeReport-1.png"
    ]
   }

 var images = JSON.stringify(jsonResponse.Images);

console.log(images);

$("#foo").append($("<li>").text("foo").attr("data-foo", images));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="foo">
</ul>

This will give you

<li data-foo="["http://some.image.com/somepath/SomeReport-0.png","http://some.image.com/somepath/SomeReport-1.png"]">foo</li>

But you need to mind the quotes as it may generate invalid html.

However, even if you had

data-attr="http://some.url.com/somepath/ReportStore/image-0.png,http://some.url.com/somepath/ReportStore/image-1.png"

which you could get server-side with a String.join() then you can use javascript split() on this to get the array back again.

var imagesArr = ($(this).data("attr")).split(",");
Jasen
  • 14,030
  • 3
  • 51
  • 68
0

Just map the images to an array of strings and return that object from your controller.

public string[] Get()
{
     //create this array object from your images object
     return new string[]{"imagePath1", "imagePath2", "imagePath3"};
}
Ronnie
  • 670
  • 6
  • 15