Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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"]"
share|improve this question

closed as unclear what you're asking by MethodMan, EZI, L.B, AstroCB, Ethaan Apr 8 at 3:11

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

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

2 Answers 2

up vote 1 down vote accepted

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(",");
share|improve this answer

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"};
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.