如何在动态JSON中替换属性值

本文关键字:替换 属性 JSON 动态 | 更新日期: 2023-09-27 18:04:38

如何替换JSON字符串中的属性值?

我有json数据如下:

{
  "httpCode" : 200,
  "message" : "OK",
  "result" : {
    "furnitureItem" : {
      "id" : "f_1150426",
      "vertical" : "furniture",
      "title" : "Antique Tribal Gashgai Persian Rug",
      "description" : "Named after the Qashqa'i tribe in Persia, these nomadic rugs represent the skillful weaving styles of the tribe. Combining unique details with traditional family motifs the rugs are heavily ornamented. They often feature geometric patterns and highly developed floral designs. The colors often feature red, saffron yellow and navy. A distinguishable mark of Qashqai antique rugs is the use of flat weave.",
      "dealerReference" : "47550",
      "dibsReference" : "LU85971150426",
      "dateAdded" : "2014-05-12T15:23:26.000-04:00",
      "dateSold" : "1969-12-31T19:00:00.000-05:00",
      "releaseDate" : "2014-05-14T11:00:00.000-04:00",
      "modifiedDate" : "2014-05-12T23:48:19.000-04:00",
      "uploadType" : "USER",
      "status" : {
        "productStatus" : "UNRELEASED",
        "contactForPrice" : "N",
        "locked" : "Y",
        "posted" : "Y",
        "newListing" : "N",
        "inSaturdaySale" : "Y",
        "saveReleaseLater" : "N"
      },
      "overrideShipping" : "N",
      "pieces" : 1,
      "material" : {
        "description" : "Wool / hand Made",
        "restricted" : "N"
      },
      "customMaterial" : {
        "restricted" : "N",
        "description" : "Wool / hand Made"
      },
      "publishOptions" : {
        "dibs" : "Y",
        "featured" : "Y",
        "nydc" : "N",
        "storefront" : "Y",
        "workInProgress" : "N",
        "listedFrenchItem" : "N",
        "addText" : "N",
        "queuedForPosting" : "N",
        "rejected" : "N",
        "approved" : "N",
        "onHold" : "N",
        "deleted" : "N",
        "sold" : "N",
        "photoRepair" : "N",
        "previouslySold" : "N",
        "unpublished" : "N"
      },
      "seller" : {
        "id" : "f_8597",
        "status" : "active"
      },
      "store" : {
        "id" : 617
      },
      "classification" : {
        "style" : { },
        "countryOfOrigin" : "Iran",
        "categories" : [ "J32" ],
        "creationDate" : "Circa 1900",
        "period" : "_1900_TO_1920"
      },
      "measurement" : {
        "unit" : "IN",
        "width" : "60",
        "length" : "87",
        "weight" : { },
        "volume" : "0.0",
        "shape" : "rectangular2d"
      },
      "condition" : {
        "state" : "EXCELLENT",
        "damageLosses" : "N",
        "damageLight" : "N",
        "damageSound" : "N",
        "damageFading" : "N"
      },
      "images" : [ {
        "position" : 1,
        "status" : "PENDING",
        "thumb" : "/archivesE/upload/8597/20_14/antiquetribalgashgaipersianrug/antiquetribalGashgaipersianrug47550nazmiyal_t.jpeg",
        "small" : "/archivesE/upload/8597/20_14/antiquetribalgashgaipersianrug/antiquetribalGashgaipersianrug47550nazmiyal_l_s.jpeg",
        "medium" : "/archivesE/upload/8597/20_14/antiquetribalgashgaipersianrug/antiquetribalGashgaipersianrug47550nazmiyal_l_m.jpeg",
        "large" : "/archivesE/upload/8597/20_14/antiquetribalgashgaipersianrug/antiquetribalGashgaipersianrug47550nazmiyal_l.jpeg"
      }, {
        "position" : 2,
        "large" : ""
      }, {
        "position" : 3,
        "large" : ""
      }, {
        "position" : 4,
        "large" : ""
      }, {
        "position" : 5,
        "large" : ""
      }, {
        "position" : 6,
        "large" : ""
      }, {
        "position" : 7,
        "large" : ""
      }, {
        "position" : 8,
        "large" : ""
      }, {
        "position" : 9,
        "large" : ""
      }, {
        "position" : 10,
        "large" : ""
      } ],
      "shipping" : [ {
        "id" : 228066,
        "quoteSource" : {
          "role" : "SELLER",
          "id" : "f_8597"
        },
        "shipmentQuoteType" : "PRE_QUOTE",
        "currency" : "USD",
        "subTotal" : 0.00,
        "totalAmount" : 0.00,
        "shipmentCostIncluded" : "Y",
        "complimentaryShipping" : "Y",
        "handlingTimeDays" : 2,
        "serviceMethod" : {
          "windowMin" : 3,
          "windowMax" : 21,
          "windowUnit" : "DAY",
          "region" : "REST_OF_WORLD",
          "methodName" : "STANDARD"
        },
        "anchor" : {
          "sourceId" : "f_1150426",
          "sourceType" : "ITEM"
        },
        "amountCurrencies" : {
          "EUR" : 0.00,
          "PND" : 0.00,
          "USD" : 0.00
        },
        "invoiceItemType" : "SHIPMENT",
        "adjustmentDocumentType" : "SHIPMENT_QUOTE"
      } ],
      "pricing" : {
        "currency" : "USD",
        "lowestPrice" : 0.0,
        "amount" : 9500.0,
        "pricePerPiece" : "N",
        "negotiable" : "Y",
        "hidePrice" : "N",
        "bargain" : "N",
        "qualifyForBargain" : "Y",
        "initialPrice" : 9500,
        "initialPriceCurrencies" : {
          "EUR" : 6745.00,
          "PND" : 5510.00,
          "USD" : 9500.00
        },
        "amountCurrencies" : {
          "EUR" : 6745.00,
          "PND" : 5510.00,
          "USD" : 9500.00
        }
      },
      "returnPolicy" : { }
    }
  }
}

我想用以下内容替换"images"属性的值:

      "images": [
        {
          "position": 1,
          "thumb": "/archivesE/upload/8597/20_14/1150426/1150426_t.jpeg",
          "small": "/archivesE/upload/8597/20_14/1150426/1150426_l_s.jpeg",
          "medium": "/archivesE/upload/8597/20_14/1150426/1150426_l_m.jpeg",
          "large": "/archivesE/upload/8597/20_14/1150426/1150426_l.jpeg"
        },
        {
          "position": 2,
          "thumb": null,
          "small": null,
          "medium": null,
          "large": ""
        },
        {
          "position": 3,
          "thumb": null,
          "small": null,
          "medium": null,
          "large": ""
        },
        {
          "position": 4,
          "thumb": null,
          "small": null,
          "medium": null,
          "large": ""
        },
        {
          "position": 5,
          "thumb": null,
          "small": null,
          "medium": null,
          "large": ""
        },
        {
          "position": 6,
          "thumb": null,
          "small": null,
          "medium": null,
          "large": ""
        },
        {
          "position": 7,
          "thumb": null,
          "small": null,
          "medium": null,
          "large": ""
        },
        {
          "position": 8,
          "thumb": null,
          "small": null,
          "medium": null,
          "large": ""
        },
        {
          "position": 9,
          "thumb": null,
          "small": null,
          "medium": null,
          "large": ""
        },
        {
          "position": 10,
          "thumb": null,
          "small": null,
          "medium": null,
          "large": ""
        }
      ],

我试过RegEx:

"images"'s*:'s*'['s*{['s'S]+}

以下是我对JSON的尝试。NET:我无法设置"images"数组的属性值

void Main()
{
    var newValue = new[] { new Test(){Position="1"}, new Test(){Position="2"}};
    string json = File.ReadAllText("E:''JSON.txt");
    dynamic rss = JObject.Parse(json);
    rss["result"]["furnitureItem"]["images"] = newValue;//this line throws error
    Console.WriteLine(rss.ToString());
}
public class Test
{
    public string Position {get;set;}
}

如何在动态JSON中替换属性值

试试这个:

JObject rss = JObject.Parse(json);
JObject furnitures = rss.SelectToken("result").SelectToken("furnitureItem");
furnitures ["images"] = newValue;

您可以使用Newtonsoft JToken来替换该值,这要容易得多,因为它支持json路径,下面是代码片段。

var parseResponse = JToken.Parse(jsonString);
var selectedPath = parseResponse.SelectToken("$.result.furnitureItem.images");
if(selectedPath!=null){
selectedPath.Replace(newValue);
}