jquery 在将JSON响应转换为Datatable C#时,完成对象化后,在JSON字符串中发现其他文本

toe95027  于 2023-10-17  发布在  jQuery
关注(0)|答案(1)|浏览(134)

我有一个JSON的响应,我试图转换成Datatable C#使用JsonConvert.DeserializeObject,但我得到错误,因为
在完成对象格式化后,在JSON字符串中发现其他文本
下面是我的JSON响应

{
  "Status": "Success",
  "ErrorCode": "00",
  "ErrMsg": "",
  "BuildingRJID": "CHNNBD0862314",
  "airFiberFeasibility": "P",
  "fiberFeasibility": "P",
  "PMPFeasibility": "V",
  "Coverage_5G": "No",
  "geoCoordinates": "null,null",
  "FloorWiseStatus": [],
  "address": {
    "buildingId": "CHNNBD0862314",
    "jioCenterId": "null",
    "jioPointId": "",
    "JCSAPId": "null",
    "JPSAPId": "I-TN-CHNN-UJP-0035",
    "bldngStatus": "NR_RFS",
    "fsaId": "",
    "fsaStatus": null,
    "csaDesign": ""
  }
}

另请参阅我将代码转换为Datatable失败的地方

try
        {
            strInputJSON_CheckRFS_Status = (new JavaScriptSerializer()).Serialize(inputCheckRFS_ID);

            WebClient client = new WebClient();
            client.Headers["Content-type"] = "application/json";
            client.Encoding = Encoding.UTF8;
            string jsonData = client.UploadString(apiUrl_CheckBldgRFS_Status, strInputJSON_CheckRFS_Status);

            DataTable dt = (DataTable)JsonConvert.DeserializeObject(jsonData, (typeof(DataTable))); // here it's failing

        }
        catch (Exception ex)
        {
            throw ex;
        }
zsohkypk

zsohkypk1#

问题是JSON对象无法转换为DataTable。除非你的数据/JSON是一个数组。
1.将jsonData读取为JObject
1.将JObject示例添加到JArray中。
1.迭代JArray作为投影。
1.将JArray示例转换为DataTable

using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

JObject jObj = (JObject)JsonConvert.DeserializeObject(jsonData);
JArray jArray = new JArray();
jArray.Add(jObj);

jArray = JArray.FromObject(jArray.AsJEnumerable()
    .Select(x => new JObject 
            { 
                { "airFiberFeasibility", x["airFiberFeasibility"] }, 
                { "PMPFeasibility", x["PMPFeasibility"] }
                // Other properties to be displayed 
            })
    .ToList());

DataTable dt = jArray.ToObject<DataTable>();

由于您希望返回数据并将其显示在HTML表中,因此没有必要将其作为DataTable(这是一种旧技术)返回。将数据返回为JArrayList<T>会更容易。

using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

JObject jObj = (JObject)JsonConvert.DeserializeObject(jsonData);
JArray jArray = new JArray();
jArray.Add(jObj);

jArray = JArray.FromObject(jArray.AsJEnumerable()
    .Select(x => new JObject 
            { 
                { "airFiberFeasibility", x["airFiberFeasibility"] }, 
                { "PMPFeasibility", x["PMPFeasibility"] } 
                // Other properties to be displayed
            })
    .ToList());

return jArray;

using System.Collections.Generic;
using Newtonsoft.Json;

ProjectionModel projection = JsonConvert.DeserializeObject<ProjectionModel>(jsonData);

List<ProjectionModel> list = new List<ProjectionModel> { projection };

return list;
public class ProjectionModel
{
    public string AirFiberFeasibility { get; set; }
    public string PMPFeasibility { get; set; }
    // Other properties
}

相关问题