user1643956
user1643956

Reputation: 21

How to convert a javascript string array to a json string array?

I'm currently writing a query that needs to send a string array to a webresource through json. This is the webresource i need to call:

[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]

Result API.ImportByNumbers(string[] _Numbers) {}

Now i dont know how to form my json data. I've tried sending a normal javascript string array with all the numbers in it, but this gives a

Could not process child error

When i try an array of objects

Data = '{"ID":"1", "ID":"2"}' 

the value inside webresource is always null...

Can anybody help?

Upvotes: 1

Views: 194

Answers (3)

user1643956
user1643956

Reputation: 21

Ok. I found the solution:

var idArray = ...        array of strings    
var Data = {"_Numbers": idArray }

then in in the query:

Data = JSON.stringify()

Upvotes: 1

Alexandr Areshnikov
Alexandr Areshnikov

Reputation: 1

Looks like the service is expecting to receive an array, not an object, so you should try not to complicate things and send something like this:

var json_str = "[1,2,3]"

But if you really need an JSON object, then you should try this

var json_str='{"_Numbers":[1,2,3]}'

Upvotes: 0

Ashirvad
Ashirvad

Reputation: 2377

Since your webmethod needs string[] _Numbers SO you have to pass such a json data which will send string of number .. Something like this.

var string={"1","2","3"};

var jsonData="{"+"_Numbers:"+"'"+string+"'"+"}"

Upvotes: 0

Related Questions