Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am looking at this example (example 8) how to send or post multiple Json objects. I wanted to pass an entity type model but not sure how using the said example.

C#
[Route("example8")]
[HttpPost]
public string GetListsFromBody([FromBody] List<List<ParamsObject>> paramsList)
{
  if (paramsList != null)
  {
    return "recieved a list with length:" + paramsList.Count;
  }
 
  return "NOTHING RECIEVED...";
}

JSON with 2 objects
JavaScript
[
 [
  {"Id1":3,"Id2":76,"Id3":19},
  {"Id1":56,"Id2":87,"Id3":94},
  {"Id1":976,"Id2":345,"Id3":7554}
 ],
 [
  {"Id1":3,"Id2":76,"Id3":19},
  {"Id1":56,"Id2":87,"Id3":94},
  {"Id1":976,"Id2":345,"Id3":7554}
 ]
]

I currently have this applied on my code. But I can't do db.Sales_.Add(Sales); It's saying it has an overload error.
C#
[ResponseType(typeof(Sales))]
public HttpResponseMessage PostSales([FromBody]List<List<Sales>> Sales, [FromUri] string auth)
{
    try
    {
        if (ModelState.IsValid)
        {
          db.Sales_.Add(Sales);
          db.SaveChanges();
          return Request.CreateErrorResponse(HttpStatusCode.OK, "Added!");
         }
            else { //...
Posted
Updated 21-Apr-17 10:47am
v2

1 solution

In principle, you can send any content in HTTP request, it only need to be serializable into a string. So, it could be multiple JSON object. I don't even understand where you could get stuck.

However, you are not trying to send 2 or more. And that is reasonable. What you called "JSON with 2 objects" is nothing but just one JSON string which will be parsed into one object. This object is the top-level array. You cannot even say "with 2 objects". It's one object with two element objects, inner array, each having three element objects, each having three properties.

Therefore, the problem you are concerned with simply does not exist. And if it ever appears, you can always reduce it to one object, by wrapping your N objects in an outer object having two properties. Exactly as in your JSON sample.

Some background: perhaps you need to understand that JavaScript is a loose-type language; and all arrays and '{...}' objects are exactly the same things of the "type" 'object': associative containers (arrays) providing access to the elements by keys of any types. The only difference an array makes is that it has the length property. Please see:
http://en.wikipedia.org/wiki/JavaScript#Dynamic[^],
http://en.wikipedia.org/wiki/Associative_array[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array[^].

See also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON[^].

—SA
 
Share this answer
 
Comments
Joshua Masa 29-Apr-15 11:33am    
Thank you for the information. I'm not that great with the terms. But how can I pass Sales to db.Sales_.Add();? It keeps saying that it has invalid arguments.
Sergey Alexandrovich Kryukov 29-Apr-15 12:09pm    
I already answered your question, not just sent you "information". Will you accept it?
As to the passing Sales, you are already passing it; what's the problem?
—SA
Joshua Masa 29-Apr-15 12:12pm    
On my db.Sales_.Add(Sales); It's saying it has an overload error. Its like I'm missing something.
Sergey Alexandrovich Kryukov 29-Apr-15 13:39pm    
No, there is no such thing as "overload error". It's just the signature of the method you are trying to call does not match any possible interpretation of your actual parameters. It's way too simple: look at what parameters are types are required by the signature, and make sure you supply matching number of parameters of assignment-compatible types.
As you did not show the declaration of this method, I cannot help you here; you have to figure out this simple things yourself. Perhaps it does not match your type parsed from JSON, but then you need to extract the data you need and supply it in the form the call requires, because the signature if the method you are trying to call is the master.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900