0

I have Developed the C# Web Method. In this Method Request and Response are Json Array Format.

When I Read Json Array from Post Request, Error Occurred.

My Json Array is

[{"partner_hotel_code": "510","reservation_id": "7660"},{"partner_hotel_code": "510","reservation_id": "7666"}]

Error is

"Type 'System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' is not supported for deserialization of an array."

When i changed the Json into below mentioned format, My method is working Properly.

{"JsonData":[{"partner_hotel_code": "510","reservation_id": "7660"},{"partner_hotel_code": "510","reservation_id": "7666"}]}

But I don't want this format.

Please help, how to rectify the Problem.

Vicky
  • 3
  • 6

3 Answers3

0

You can map that JSON array to List of C# class.

public class RootObject
{
    public string partner_hotel_code { get; set; }
    public string reservation_id { get; set; }
}

In the web method add parameter List<RootObject> ObjectList

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
  • Hi Jerry, I can't add Parameters for the Web Method. Because of The Third Party Call this function like **booking_sync()** and not like **booking_sync(List ObjectList)** – Vicky Apr 12 '17 at 06:37
  • if it's parameterless then how are you getting jsonarray – GorvGoyl Apr 12 '17 at 06:41
  • also add that method to the question – GorvGoyl Apr 12 '17 at 06:41
  • Below is my code `[WebMethod] public void booking_sync() { string postData = new System.IO.StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd(); var jss = new JavaScriptSerializer(); var dictionary = jss.Deserialize[]>(postData);}` – Vicky Apr 12 '17 at 06:47
  • in this case deserialize postdata to list using newtonsoft library – GorvGoyl Apr 12 '17 at 06:50
  • I guess you can use JavaScriptSerializer also – GorvGoyl Apr 12 '17 at 06:51
  • How to deserialize this postdata – Vicky Apr 12 '17 at 06:53
  • var list = jss.Deserialize>(postData); //or see this http://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp – GorvGoyl Apr 12 '17 at 06:55
0

You are getting this error because the Json parameter is required to be an object of key-value pairs, that is:

{"JsonData":[{"partner_hotel_code": "510","reservation_id": "7660"},{"partner_hotel_code": "510","reservation_id": "7666"}]}

{'Key': Value} => Key - JsonData, Value => array of items

With this in mind, you can make your models to match this structure as follows: Prepare a model Item,

public class Item
{
    public string partner_hotel_code { get; set; }
    public string reservation_id { get; set; }
} 

Then prepare a root 'Node' object that will take in a list of the items, let's call it Reservations:

 public class Reservations
    {
       List<Item> JsonData { get; set; }
    }

You can then deserialize as shown below:

var data = new JavaScriptSerializer().Deserialize<Reservations>(postData);

But the structure you wanted is something like: [{"partner_hotel_code": "510",...}]

You can achieve this by many ways, an example is using a foreach:

var list = new List<Item>();

foreach(var item in data.JsonData)
{
  list.Add(item);
}

/*The value of list here will contain the items*/

listArray[] myArray = list.ToArray();
Felix Too
  • 11,614
  • 5
  • 24
  • 25
  • Hi Felix, For Testing Purpose, I have manually send the Json Array Using Postman Rest Client. At this time the web method is not triggered. Only error response is coming. The Error message posted above. – Vicky Apr 12 '17 at 08:20
  • Did you try to make the changes I outlined above? You must send the array in the format `{"JsonData":[{"partner_hotel_code": "510",...]` because it has to be a key-value pair. Without a key-value pair, you'll get the error you're getting. – Felix Too Apr 12 '17 at 08:44
0

another solution to this problem is to serialize your array first, then create a JSON object with your method parameter name, and Serialize that... it goes something like this

var myobj = { myArray: JSON.stringify(a) };
passedData = JSON.stringify(myobj);
$.ajax({
        type: "POST", contentType: "application/json", data: passedData,
        url: window.location.href + "/myMethod",
        success: function (result) {
           ....
        }
});

and the webMethod

public static void myMethod(String myArray)
{
   dynamic jsonResponse = JsonConvert.DeserializeObject(myArray);
   for (var i = 0; i < jsonResponse.Count; i++)
   {
       ...........
   }
}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Serge
  • 1