首页 > 解决方案 > 从json字符串中删除一个字段

问题描述

我将 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 文本,

JavaScriptSerializer().DeserializeObject(x.Data.Replace("", ""))

我想从中删除违规字段,请提供任何帮助。提前致谢

这是我的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"
   }
}

上面的这个对象是一个作为字符串返回的动态对象,我想从中删除 Violation 相关字段及其值。我想从上面的 json 对象中删除与 Violation 相关的 Violations、ViolationTypeId 等字段,请您在这方面帮助我,谢谢。

标签: c#json

解决方案


您可以简单地将其转换为对象,删除字段并返回字符串。

例如在 javascript

假设你的 json 在变量 myjson

tmp = Object(myjson.Data)
delete tmp.Violations
myjson.Data = JSON.stringify(tmp)

在 C# 中,假设 Data 是你的字符串 json

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

推荐阅读