C# 从jSon字符串中删除字段

C# 从jSon字符串中删除字段,c#,json,C#,Json,我将jSon字符串存储为十六进制或一些字符串,当我在代码中检索为jSon字符串时,一切正常,但在检索后,我想从jSob对象中删除一个字段 var t = StoredProcedures.SP_GlobalSearch.Execute(new SP_GlobalSearch_Params { Search = request.search, SourceKe

我将jSon字符串存储为十六进制或一些字符串,当我在代码中检索为jSon字符串时,一切正常,但在检索后,我想从jSob对象中删除一个字段

  var t = StoredProcedures.SP_GlobalSearch.Execute(new SP_GlobalSearch_Params
                    {
                        Search = request.search,
                        SourceKey = "",
                        Skip = start,
                        Take = length,
                        ShowDeleted = false
                    }).Select(x => new SP_GlobalSearch
                    {
                        JsonObject = x.Data != null ? new JavaScriptSerializer().DeserializeObject(x.Data.Replace("", "")) : null,
                        Date = x.Date,
                        JsonObjectId = x.JsonObjectId,
                        SourceKey = x.SourceKey,
                        SourceId = x.SourceId,
                        TotalCount = x.TotalCount
                    });

                    var response = t?.ToList();

                    var jsondata = new DataTableResponse<SP_GlobalSearch>
                    {
                        totalCount = (int)response.Where(x => x.TotalCount != null).FirstOrDefault().TotalCount,
                        data = response.Where(x => x.TotalCount == null)
                    };

                    var jsonResult = Json(jsondata);
                    jsonResult.MaxJsonLength = Int32.MaxValue;
                    return jsonResult;
我想从中删除违规字段,请提供帮助。提前谢谢

这是我的json:

{
   "InspectionResultId":846,
   "InspectionResultNumber":"T00846",
   "InspectionRequestId":507,
   "InspectionRequestNumber":"R00507",
   "CaseId":689,
   "InspectionResultStatusId":605,
   "EnforcementSectionId":104,
   "ViolationTypeId":603,
   "DateOfInspection":"\/Date(1589439600000)\/",
   "InspectionComment":"send to office staff, open investigation",
   "InspectedCompanyDataId":964,
   "ContactTypeId":701,
   "EnteredById":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
   "InspectionResultTimestamp":"\/Date(1588280817470)\/",
   "DateCreated":"\/Date(1588280817487)\/",
   "DateUpdated":"\/Date(1588281867967)\/",
   "CreatedByUserId":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
   "UpdatedByUserId":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
   "Case":{
      "RelatedContactId":0,
      "CaseId":689,
      "CaseNumber":"I00689",
      "IsInvestigation":true,
      "CaseStatusId":801,
      "InspectionItemSourceId":211,
      "EnforcementSectionId":104,
      "ReminderDate":"\/Date(1590044400000)\/",
      "PreCaseSummary":"send to office staff, open investigation",
      "AssignedToInspectorId":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
      "CaseTimestamp":"\/Date(1588281867403)\/",
      "CaseCompanyId":964,
      "DateCreated":"\/Date(1588281867437)\/",
      "DateUpdated":"\/Date(1588281867840)\/",
      "CreatedByUserId":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
      "UpdatedByUserId":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
      "IsDeleted":false,
      "InterpreterNeeded":false,
      "VoluntaryDisclosure":false,
      "PenaltyAdjustment":0,
      "Company":{
         "ContactId":964,
         "ContactTypeId":1000,
         "FirstName":"04.30.2020 new co",
         "Notes":"new request",
         "Active":true,
         "Created":"\/Date(1588279909680)\/",
         "Updated":"\/Date(1588279909680)\/",
         "DateCreated":"\/Date(1588279909680)\/",
         "DateUpdated":"\/Date(1588279909680)\/",
         "IsEJArea":false,
         "CreatedByUserId":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
         "UpdatedByUserId":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
         "Classification":{

         },
         "Class":"Company",
         "IsSpecial":false,
         "Address":{
            "Street1":"5678 Street",
            "City":"Sacramento",
            "StateCode":"CA",
            "Zip":"95812",
            "Country":"0"
         }
      }
   },
   "InspectionResultStatus":{
      "InspectionResultStatusId":605,
      "InspectionResultStatusName":"Clean",
      "InspectionResultStatusSortOrder":5
   },
   "EnforcementSection":{
      "EnforcementSectionId":104,
      "EnforcementSectionName":"STBES",
      "EnforcementSectionSortOrder":0,
      "BranchId":2
   },
   "InspectedCompanyData":{
      "ContactId":964,
      "ContactTypeId":1000,
      "FirstName":"04.30.2020 new co",
      "Notes":"new request",
      "Active":true,
      "Created":"\/Date(1588279909680)\/",
      "Updated":"\/Date(1588279909680)\/",
      "DateCreated":"\/Date(1588279909680)\/",
      "DateUpdated":"\/Date(1588279909680)\/",
      "IsEJArea":false,
      "CreatedByUserId":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
      "UpdatedByUserId":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
      "Classification":{

      },
      "Class":"Company",
      "IsSpecial":false
   },
   "InspectionItems":[
      {
         "InspectionItemId":1054,
         "InspectionItemNumber":"T00000",
         "InspectionRequestId":507,
         "InspectionResultId":846,
         "CaseId":689,
         "InspectionItemSourceId":229,
         "IsCreatedInCase":false,
         "ShowYearMakeModel":"",
         "ShowYearMakeModelVIN":"",
         "GrossWeight":0.00,
         "RegHoldSet":false,
         "InspectionItemTimestamp":"\/Date(1588280802553)\/",
         "DateCreated":"\/Date(1588280802567)\/",
         "DateUpdated":"\/Date(1588281868153)\/",
         "CreatedByUserId":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
         "UpdatedByUserId":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
         "Violations":[

         ]
      }
   ],
   "CompletedBy":[
      {
         "IsActive":true,
         "FirstName":"Daphne",
         "LastName":"Greene",
         "ObjectGUID":"S-1-5-21-1538631513-416410304-3002070310-33442",
         "NameLoginFormat":"dgreene",
         "NameFirstLast":"Daphne Greene",
         "NameLastFirst":"Greene Daphne",
         "Email":"daphne.greene@arb.ca.gov",
         "EmailConfirmed":false,
         "SecurityStamp":"e061e77e-c93e-4b9e-8497-4852b5cb4ca2",
         "PhoneNumberConfirmed":false,
         "TwoFactorEnabled":false,
         "LockoutEnabled":false,
         "AccessFailedCount":0,
         "Id":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
         "UserName":"dgreene"
      }
   ],
   "EnteredBy":{
      "IsActive":true,
      "FirstName":"Daphne",
      "LastName":"Greene",
      "ObjectGUID":"S-1-5-21-1538631513-416410304-3002070310-33442",
      "NameLoginFormat":"dgreene",
      "NameFirstLast":"Daphne Greene",
      "NameLastFirst":"Greene Daphne",
      "Email":"daphne.greene@arb.ca.gov",
      "EmailConfirmed":false,
      "SecurityStamp":"e061e77e-c93e-4b9e-8497-4852b5cb4ca2",
      "PhoneNumberConfirmed":false,
      "TwoFactorEnabled":false,
      "LockoutEnabled":false,
      "AccessFailedCount":0,
      "Id":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
      "UserName":"dgreene"
   },
   "MyGridContacts":[

   ],
   "CreatedByUser":{
      "IsActive":true,
      "FirstName":"Daphne",
      "LastName":"Greene",
      "ObjectGUID":"S-1-5-21-1538631513-416410304-3002070310-33442",
      "NameLoginFormat":"dgreene",
      "NameFirstLast":"Daphne Greene",
      "NameLastFirst":"Greene Daphne",
      "Email":"daphne.greene@arb.ca.gov",
      "EmailConfirmed":false,
      "SecurityStamp":"e061e77e-c93e-4b9e-8497-4852b5cb4ca2",
      "PhoneNumberConfirmed":false,
      "TwoFactorEnabled":false,
      "LockoutEnabled":false,
      "AccessFailedCount":0,
      "Id":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
      "UserName":"dgreene"
   },
   "UpdatedByUser":{
      "IsActive":true,
      "FirstName":"Daphne",
      "LastName":"Greene",
      "ObjectGUID":"S-1-5-21-1538631513-416410304-3002070310-33442",
      "NameLoginFormat":"dgreene",
      "NameFirstLast":"Daphne Greene",
      "NameLastFirst":"Greene Daphne",
      "Email":"daphne.greene@arb.ca.gov",
      "EmailConfirmed":false,
      "SecurityStamp":"e061e77e-c93e-4b9e-8497-4852b5cb4ca2",
      "PhoneNumberConfirmed":false,
      "TwoFactorEnabled":false,
      "LockoutEnabled":false,
      "AccessFailedCount":0,
      "Id":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
      "UserName":"dgreene"
   }
}
上面的这个对象是一个动态对象,返回为string,我想从中删除与冲突相关的字段及其值。
我想从上面的jSon对象中删除与违规相关的违规、违规类型ID等字段,请您在这方面帮助我,谢谢。

您只需将其转换为object,删除字段并返回字符串即可

例如在javascript中

假设您的json在变量myjson中

tmp = Object(myjson.Data)
delete tmp.Violations
myjson.Data = JSON.stringify(tmp)
在C#中,假设数据是字符串json

JObject tmp = JObject.Parse(Data);
tmp.Property("Violations").Remove();
Data = JavaScriptSerializer().Serialize(jsonResult.Data);

我认为向我们展示json的输出也会更容易。我想从中删除违规字段-我们如何告诉您如何做到这一点而不看到json本身?如果你想把问题包括在内,这里更有可能有人能帮你。请参阅:。您好,我已通过编辑将jSong示例字符串添加到原始帖子中-感谢您的回复。您为什么要使用Microsoft提供的
JavaScriptSerializer
?为什么不呢?我不确定你能不能帮我更多的忙,这是一个动态对象改变了我怎么才能从上面的jSon中删除违规和违规类型ID等字段请我想在C中删除它?我能说你爱你吗?兄弟,很高兴帮助兄弟!你可以把它标记为已回答✅ 如果这个答案对你有帮助。
JObject tmp = JObject.Parse(Data);
tmp.Property("Violations").Remove();
Data = JavaScriptSerializer().Serialize(jsonResult.Data);