接收JSON数据处理:
JSON数组数据类型不一至:
```csharp
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Http;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace GreatDevelop_APi1.ApiControllers
{
public class CustomController : ApiController
{
public CustomController()
{
}
[System.Web.Mvc.HttpPost]
public IHttpActionResult Add([FromBody] object map)//使用object类型接收JSON数据
{
string ls_message = "传参数不正确";
if (map == null)
{
return Json(ls_message);
}
string json = map.ToString();//将JSON数据Tostring
JObject jo = (JObject)JsonConvert.DeserializeObject(json);//将字符串反序列化,转JSON类型
ControlInfo controlinfo = new ControlInfo();//实例化ControlInfo类
object a = controlinfo.SSystem;
a = jo["ControlInfo"].Value<object>("SSystem");//用objevt类型收受JSON类型数据中对应的值
object b = controlinfo.DSystem;
b = jo["ControlInfo"].Value<object>("DSystem");
string strJSON = "{\"A\":\"" + a + "\",\"B\":\"" + b + "\"}";//生成返回json字符串
JObject jo1 = (JObject)JsonConvert.DeserializeObject(strJSON);//转化为JSON类型数据
return Json(jo1);//输出JSON类型数据{"A":"a","B":"b"}
}
public class ControlInfo
{
public object SSystem { get; set; }
public string DSystem { get; set; }
}