Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have developed Webmethod in C#. Here server send the Json Array in POST Request in the Below format.
HTML
[{ "name" : "AAAA", "address" : "Chennai"},{"name": "BBBB", "address":"Trichy"}]

How to read this Json and Serialize this Json?

What I have tried:

When I Change the Request to below type my code is working Properly.
"JsonData":{[{ "name" : "AAAA", "address" : "Chennai"},{"name": "BBBB", "address":"Trichy"}]}

But my request was not like this.
Posted
Updated 11-Apr-17 23:57pm
Comments
Karthik_Mahalingam 7-Apr-17 8:24am    
post the code
Member 12976241 7-Apr-17 8:45am    
[System.Web.Services.WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet=false)]
[WebInvoke(ResponseFormat=WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Bare, Method="POST")]
public void booking_sync()
{
var req = new StreamReader(HttpContext.Current.Request.InputStream);
req.BaseStream.Seek(0, SeekOrigin.Begin);
dynamic body = req.ReadToEnd();

//The Below Code was used when i change the request format
var token = JToken.Parse(body);
var data = token.Value<jarray>("JsonData");
}
Karthik_Mahalingam 7-Apr-17 8:54am    
does this send any data to client?
Member 12976241 7-Apr-17 9:22am    
Yes, Mr.Kathick. I want to send the Some other Json Array to Client
Karthik_Mahalingam 7-Apr-17 9:26am    
show how you are getting json value to display

Your data is not in a correct format, it must be enclosed in curly braces { and }. See JSON Introduction[^].
 
Share this answer
 
Comments
Member 12976241 7-Apr-17 9:24am    
hi Mr.Richard, Thanks for your reply.I want to receive the Json Array. This is the correct format for Json Array.
Richard MacCutchan 7-Apr-17 9:45am    
Yes, but it is not the correct format for a JSON string, which must be enclosed in curly braces.
Member 12976241 7-Apr-17 9:48am    
ok, But the third party given the request in this type only.
Richard MacCutchan 7-Apr-17 9:57am    
Then you need to tell them that the data is incorrect.
try
string json  = "[{ \"name\" : \"AAAA\", \"address\" : \"Chennai\"},{\"name\": \"BBBB\", \"address\":\"Trichy\"}]";
           json = "JsonData:{" +  json + "}";
 
Share this answer
 
Comments
Member 12976241 12-Apr-17 7:02am    
Thanks for your Solution. When I assign the values manually inside the coding, it is working. But When i receive the value from outside, it is not working.
Karthik_Mahalingam 12-Apr-17 7:16am    
what do you mean by " But When i receive the value from outside, "
Member 12976241 12-Apr-17 8:05am    
Outside means, When the third Party calling this function, at the time they will send some post request. this request contains json array in the body content.

The Request format is
https://gracebeta.com/PMSDEV/TripIBAPI/InstantBooking.asmx/booking_sync -d '[{"reservation_id":"123","partner_hotel_code":"221"}]' -H "Content-Type:application/json"
Karthik_Mahalingam 12-Apr-17 8:16am    
so how will you extract the json
Member 12976241 12-Apr-17 8:34am    
By using this url, i am unable to get the json array.

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