【问题标题】:Read JSON file and convert only specific tags to XML读取 JSON 文件并仅将特定标签转换为 XML
【发布时间】:2017-11-02 20:02:51
【问题描述】:

我使用 newtonsoft 将对象序列化为 JSON。这是一个记录:

   "Properties": {
      "ProductId": "e2cba925-0720-465a-8c84-79626e9869e5",
      "LinkName": "link",
      "Brand": "brandx",
      "SiteActive": true,
      "RetailActive": true,
      "BaseColor": null,
      "BaseTextColor": null,
      "BackGroundImageId": null,
      "ProductGroupId": null,
      "MadeInUSAID": null
    },
    "Display": {
      "ProductId": "e2cba925-0720-465a-8c84-79626e9869e5",
      "ShowWeb": false,
      "ShowMobile": true,
      "ShowDatavault": true,
      "ShowDataVaultForPartners": true,
      "ShowNewsroom": false
    },
    "Image": {
      "ImageId": "e11ef84d-3c96-4fd9-a765-1f37e38ebc1a",
      "ImageThumbnailId": "dfd87a61-9d59-4a46-8895-541a21e73b39",
      "MD5": "3DECCFAA34946E1542BCCAD4DAC42CEC",
      "SHA": null,
      "SHA2_256": null,
      "SHA2_512": null,
      "DocumentType": null,
      "ContentType": "image/jpeg",
      "MaxWidth": null,
      "MaxHeight": null,
      "MaxResolution": null,
      "FileExtension": null,
      "FileName": "60144-1"
    },
    "ProductId": "e2cba925-0720-465a-8c84-79626e9869e5",
    "SKU": "60144",
    "ReceiptName": "blah blah blah",
    "UPC": "081483803371",
    "Taxable": true,
    "ColorId": "46a809ab-ac78-44f2-bd62-303345e9ff32",
    "ProductType": 1,
    "PackQty": 1
  },
  {
    "Description": {
      "ProductDescriptionId": "1b1d812b-0568-41a6-a0ed-4488c32b66e0",
      "ProductId": "2dc47c3e-7780-4768-bbae-6a1d6c4067ce",
      "LanguageId": 57,
      "Name": "widget 1",
      "TitleTag": "widget 1",
      "SEOText": null,
      "Description": null,
      "MetaDescription": null,
      "InternalKeywords": null
    }

我只需要从 JSON 中挑选某些字段并将这些值放入 XML 文档中。环顾四周,我看到很多将整个 JSON 转换为 XML 的示例,但我只需要特定的标签。有这样做的例子吗?

谢谢。

【问题讨论】:

  • 你试过探索这个库吗:nuget.org/packages/Moonmile.ExDoc.Json
  • 如果你只需要特定的值,难道你不能从你的 JSON 中获取它们,创建相应的对象来表示它们,然后对该对象进行 XML 化吗?
  • 我可以使用我需要的值创建一个对象,我只需要知道如何将 JSON 中的值提取到我的对象中。 JSON用于多个项目,我只需要一个子集。例如,我需要名称、品牌。 SKU、UPC、描述......以及其他一些。我正在尝试获取这些物品。

标签: c# json xml


【解决方案1】:

只需创建一个包含您需要的所有属性的类:

public class MyClass 
{
    public string ImageId { get; set; } // just pick the ones you truly need
}

然后在您的解析器逻辑中执行以下操作:

var serializer = new JavaScriptSerializer();
var myClassObject = serializer.Deserialize<MyClass>(json);

var xmlSerializer = new XmlSerializer(typeof(MyClass));
using (var sww = new StringWriter())
{
    using (XmlWriter writer = XmlWriter.Create(sww))
    {
        xmlSerializer.Serialize(writer, myClassObject);
        var xmlDocument = sww.ToString(); // Your XML
    }
}

假设 json 是您在帖子中发布的 json 文档。

【讨论】:

    【解决方案2】:

    使用@BrianRogers 回答我的问题,因此您只需将特定属性映射到您的类:

    Can I specify a path in an attribute to map a property in my class to a child property in my JSON?

    【讨论】:

      【解决方案3】:

      我试图过度思考这一点。我所做的是将 JSON 反序列化回对象,然后循环遍历对象,从对象中提取我需要的项目,而不是尝试直接转到 JSON:

        string jSonProducts = File.ReadAllText(settings.productJsonConfig.JSONProductFilePath);
      
              ICollection<ProductSearchModel> prods = null;
              prods = JsonConvert.DeserializeObject<ICollection<ProductSearchModel>>(jSonProducts);
      

      这给了我一个强类型的对象来使用,它将反映对象中 JSON 的任何更改。

      【讨论】:

        【解决方案4】:

        你可以试试,Cinchoo ETL - 一个用于读写 CSV /JSON/Xml 文件的开源库

        下面的示例展示了如何从 JSON 文件中提取 2 个属性并生成 xml 输出

        using (var reader = new ChoJSONReader("sample7.json")
            .WithField("ProductId", jsonPath: "$.Properties.ProductId", fieldType: typeof(string))
            .WithField("ImageId", jsonPath: "$.Image.ImageId", fieldType: typeof(string))
        )
        {
            using (var xWriter = new ChoXmlWriter("sample7.xml").WithXPath("Products/Product"))
                xWriter.Write(reader);
        }
        

        输出 Xml:

        <Products>
          <Product>
            <ProductId>e2cba925-0720-465a-8c84-79626e9869e5</ProductId>
            <ImageId>e11ef84d-3c96-4fd9-a765-1f37e38ebc1a</ImageId>
          </Product>
        </Products>
        

        希望它对您的需求有所帮助。

        免责声明:我是这个库的作者

        【讨论】:

        • 我会试试这个,让你知道。这看起来很有希望,谢谢。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-02
        • 2020-04-19
        • 2021-10-14
        • 2021-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多