I need to capture/deserialize in C#, using only built-in .NET library, the JSON formatted POST data received from a JavaScript file.
The JSON format is:
{"URLs":[{"url_name":"Google", "url_address":"http://www.google.com/"}, {"url_name":"Yahoo", "url_address":"http://www.yahoo.com/"},{"url_name":"FB", "url_address":"http://www.fb.com/"},{"url_name":"MegaSearches", "url_address":"http://www.megasearches.com/"}]}
The JavaScript file POSTs the JSON data in http://www.mysite.com/json.aspx and I need to capture the JSON data in .aspx code-behind in C# and save to database.
First, create classes to hold the URL data, like this:
public class UrlData
{
public List<Url> URLs {get;set;}
}
public class Url
{
public string url_address {get;set;}
public string url_name {get;set;}
}
Now you can deserialize the JSON data into the objects, like this:
UrlData theUrlData = new JavaScriptSerializer().Deserialize<UrlData>(jsonResult);
Note: jsonResult
is the JSON data returned from where you are getting the data from.
Create a view model class for the incoming JSON data.
public class UrlHelper {
public string url_name {get;set;}
public string url_address {get;set;}
}
In the method in your code behind have the post method accept the data.
public YourJsonFormPost(List<UrlHelper> URLs){
//do your work
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.