-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"]"
5
  • possible duplicate of Deserializing JSON into string array Commented Apr 7, 2015 at 21:41
  • How about just using the javascript join() function? response.Images.join(", "). Commented Apr 7, 2015 at 21:46
  • hmm that's an interesting idea, I will give that a shot. Commented Apr 7, 2015 at 21:48
  • @Jasen Unfortunately that did not work, I need the array returned as a string itself. Commented Apr 8, 2015 at 0:36
  • It is possible in c# as well msdn.microsoft.com/en-us/library/… Commented Apr 8, 2015 at 0:42

2 Answers 2

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(",");
Sign up to request clarification or add additional context in comments.

Comments

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"};
}

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.