-1

I have an array of objects

let arr = [{"1":"bar"},{"2":"bar"}]

which gets sent to a service through ajax inside data

the service will then get the array & do stuff.

[WebInvoke]
public void getStuff(params Model[] data)
{
  // do stuff
}

what would my model need to look like to receive the data arr?

Update:

changed keys in object

test
  • 107
  • 12
  • no would be for each object. I'll update – test Oct 12 '17 at 12:04
  • Does https://stackoverflow.com/questions/15001755/pass-a-javascript-map-to-json-wcf-service or https://stackoverflow.com/questions/8372076/post-json-dictionary-without-key-value-text help? – mjwills Oct 12 '17 at 12:06
  • 2
    @test `[{"1":"bar"},{"2":"bar"}]` is not a de-serializable JSON object. The first part should always be the same, unless that's a `Dictionary` – Camilo Terevinto Oct 12 '17 at 12:06
  • I had JSON.stringify in my ajax data value but I didn't add it to bloat the question. This solved my problem for anybody with the same issue https://stackoverflow.com/questions/15570317/sending-array-of-objects-to-wcf – test Oct 12 '17 at 16:00

2 Answers2

0

You can do something like this to get the params:

var key = Request.Params[0];

then you can use the var "key" to fill a model

-1

The elemenst in this .js array:

let arr = [{"foo":"bar"},{"foo":"bar"}]

Could be represented as this .cs class

class Model 
{
    public string foo;
}

Because for each object (the bit inside the {}), the default is to map the lhs to a property of the class with the same name (foo).

But, after your edit, if you want this:

let arr = [{"1":"bar"},{"2":"bar"}]

Then that cannot map to a class so easily, not leat because you can't have a field named '1' but also because it implies that there are many many different options for the lhs of the json.

In that case, consider using a Dictionary

mikelegg
  • 1,197
  • 6
  • 10