Use JSON. Net to convert a JSON expression to an object

Source: Internet
Author: User

When using ASP. NET Web APIs, you want the parameters to be of the List <t> type,

It is known that Web APIs support parameters in JSON format, such as {key1: value1, key2: value2}, and the object parameters passed to the function, for example:

public Class MyClass{public string key1{get;set;}public string key2{get;set;}}...public Class BLL_GetValue{  [HTTPPost]  public void GetValue(MyClass c){    ...  }}...$.ajax({        type: 'post',        url: '/api/BLL_GetValue/GetValue',        data: JSON.Stringify({ 'key1': value1, 'key2': value2 }),        dataType: 'json',        success: function (data) {}})

 

However, if the getvalue method is as follows:

[HttpPost]public void GetValue(List<MyClass> c){    ...}

It cannot be solved. I don't know if there is any solution.

So detour:

Let the method accept the JSON string, convert it to an object using JSON. net, and pass it to the method call.

using Newtonsoft.Json;using Newtonsoft.Json.Converters;using Newtonsoft.Json.Linq;...List<Model_WI_SEARCH_CONDITION> list = new List<Model_WI_SEARCH_CONDITION>();            JArray o;            o = JsonConvert.DeserializeObject<JArray>(searchConditon);            foreach (var item in o)            {                Model_WI_SEARCH_CONDITION c = new Model_WI_SEARCH_CONDITION();                c.WI_LIST_ID = Convert.ToDecimal(item["WI_LIST_ID"].ToString());                c.MI_OPTIONS_TYPE_ID = Convert.ToDecimal(item["MI_OPTIONS_TYPE_ID"].ToString());                c.MI_OPTIONS_ID = Convert.ToDecimal(item["MI_OPTIONS_ID"].ToString());                list.Add(c);            }...

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.