读取JSON文件并仅将特定标记转换为XML

时间:2017-11-02 20:02:51

标签: c# json xml

我使用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的例子,但我只需要特定的标签。有这样做的例子吗?

感谢。

4 个答案:

答案 0 :(得分:0)

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

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文档。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

  string jSonProducts = File.ReadAllText(settings.productJsonConfig.JSONProductFilePath);

        ICollection<ProductSearchModel> prods = null;
        prods = JsonConvert.DeserializeObject<ICollection<ProductSearchModel>>(jSonProducts);

这为我提供了一个强类型对象,它将反映对象中JSON的任何更改。

答案 3 :(得分:0)

您可以尝试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>

希望它能帮助您满足您的需求。

免责声明:我是这个图书馆的作者