将嵌套的JSON反序列化为C#

时间:2018-11-15 05:26:23

标签: json asp.net-web-api

我正在从如下所示的API返回JSON:

    [
       {
          "CheckListId":"4",
          "Bphase":"k",
          "ActivityID":"2",
          "FloorTypeId":"216",
          "CheckerId":"",
          "columnFooting":"tets",
          "SubActivityID":"52",
          "columnFootingOTHER":"",
          "rejectFlag":"2",
          "Reportingtype":"Column-Floor",
          "Dphase":"",
          "UserID":"34a86e95-be70-4c6e-84b4-90fc2680cb44",
          "dateTimeSpanCre":"11/13/2018 12:00:00 AM",
          "contractID":"45",
          "UserIdAction":"34a86e95-be70-4c6e-84b4-90fc2680cb44",
          "drawingRef":"testing",
          "ChecklistForId":"683,687,688,689",
          "Aphase":"",
          "projectID":"21",
          "BuildingID":"49",
          "status":"p",
          "DetailsItems":[
             {
                "phase":"b",
                "yesNo":"n",
                "ImageName":"",
                "DoerComments":"",
                "UserId":"34a86e95-be70-4c6e-84b4-90fc2680cb44",
                "base64Img":"",
                "CheckersComments":"",

             },
             {
                "phase":"b",
                "yesNo":"n",
                "ImageName":"",
                "DoerComments":"",
                "UserId":"34a86e95-be70-4c6e-84b4-90fc2680cb44",
                "base64Img":"",
                "CheckersComments":"",

             }
          ]
       },
{
....},
{ ....
}
    ]

I have tried a few approaches

    public class SyncSaveCheckListDetailsMaster
    {
        public string UserID;
        public string CheckListId;
        public string projectID;
        public string contractID;
        public string BuildingID;
        public string columnFooting;
        public string drawingRef;
        public string dateTimeSpanCre;
        public string columnFootingOTHER;
        public string ChecklistForId;
        public string Reportingtype;
        public string FloorTypeId;
        public string status;
        public string rejectFlag;
        public string UserIdAction;
        public string CheckerId;
        public string Bphase;
        public string Dphase;
        public string Aphase;
        public string ActivityID;
        public string SubActivityID;
        public List<List<SyncSaveCheckListDetailsItem>> DetailsItems { get; set; }
    }

    public class SyncSaveCheckListDetailsItem
    {
        public string phase;
        public string yesNo;
        public string DoerComments;
        public string CheckersComments;
        public string UserId;
        public string ImageName;
        public string base64Img;

    }

我使用什么方法对该嵌套JSON数组进行脱盐

这应该很简单,但是由于某些原因,我无法找出正确的

我是Web开发的新手,我正在使用ASP.NET MVC 5开发Web应用程序。我需要反序列化嵌套的json

1 个答案:

答案 0 :(得分:0)

Ibubi 所述;您应该将DetailsItem属性更改为List<SyncSaveCheckListDetailsItem>,并确实查看NewtonSoft将json反序列化为对象。您可以找到详细信息here

这个问题之前也被问过多次; for example here