简体   繁体   English

如何将json字符串转换为yaml

[英]how to convert json string to yaml

I have the following code segment:我有以下代码段:

string stringXDoc = JsonConvert.SerializeObject(ConnectedItemOperations.GetConnectedItemById(id).Edits.EditableData);
var json = JsonConvert.DeserializeObject(stringXDoc);

this is the file that getting error : InvalidOperationException: Too much recursion when traversing the object graph.这是获取错误的文件:InvalidOperationException:遍历对象图时递归过多。 There are 2000+ products.有2000多种产品。

 <product> <code></code> <ws_code></ws_code> <barcode></barcode> <supplier_code></supplier_code> <name></name> <cat1name></cat1name><cat1code></cat1code> <cat2name></cat2name><cat2code></cat2code> <cat3name></cat3name><cat3code></cat3code> <category_path></category_path> <stock></stock> <price_list></price_list> <price_list_campaign></price_list_campaign> <price_special_vat_included></price_special_vat_included> <price_special></price_special> <price_special_rate></price_special_rate> <price_credit_card></price_credit_card> <currency></currency> <vat></vat> <brand></brand> <model></model> <desi></desi> <width></width> <height></height> <deep></deep> <weight></weight> <detail></detail> <images><img_item type_name=""></img_item><img_item type_name=""></img_item><img_item type_name=""></img_item><img_item type_name=""></img_item></images> <subproducts> <subproduct><VaryantGroupID></VaryantGroupID> <code></code> <ws_code></ws_code> <type1></type1> <type2></type2> <barcode></barcode> <stock></stock> <desi></desi> <price_list></price_list> <price_special></price_special> <supplier_code></supplier_code> </subproduct><subproduct><VaryantGroupID></VaryantGroupID> <code></code> <ws_code></ws_code> <type1></type1> <type2></type2> <barcode></barcode> <stock></stock> <desi></desi> <price_list></price_list> <price_special></price_special> <supplier_code></supplier_code> </subproduct><subproduct><VaryantGroupID></VaryantGroupID> <code></code> <ws_code></ws_code> <type1></type1> <type2></type2> <barcode></barcode> <stock></stock> <desi></desi> <price_list></price_list> <price_special></price_special> <supplier_code></supplier_code> </subproduct><subproduct><VaryantGroupID></VaryantGroupID> <code></code> <ws_code></ws_code> <type1></type1> <type2></type2> <barcode></barcode> <stock></stock> <desi></desi> <price_list></price_list> <price_special></price_special> <supplier_code></supplier_code> </subproduct></subproducts> . . . </product>

I want to convert json variable into yaml .我想将 json 变量转换为yaml stringXDoc is not same all time so that I cannot create class for DeserializeObject<> or can I ? stringXDoc 并不总是相同的,所以我不能为DeserializeObject<>创建类,或者我可以吗? Is there any way to convert this variable to yaml ?有没有办法将此变量转换为yaml

The easiest way is to use the YamlDotNet library.最简单的方法是使用 YamlDotNet 库。 Here's a code example to convert youe object to YAML format:下面是一个将你的对象转换为 YAML 格式的代码示例:

var yourObject = ConnectedItemOperations.GetConnectedItemById(id).Edits.EditableData;
var serializer = new SerializerBuilder().Build();
var yamlString = serializer.Serialize(yourObject);

I tested this with following code:我用以下代码对此进行了测试:

class User
{
    public string Name { get; set; }
    public Car[] Cars { get; set; }
}

class Car
{
    public string Make { get; set; }
    public int Year { get; set; }
}

var user = new User
{
    Name = "Me",
    Cars = new Car[]
    {
        new Car { Make = "Car1", Year = 2000 }, 
        new Car { Make = "Car2", Year = 2017 }
    }
};
var serializer = new SerializerBuilder().Build();
var asYaml = serializer.Serialize(user);

This produces following YAML string:这会产生以下 YAML 字符串:

Name: Me姓名:我

Cars:汽车:

  • Make: Car1品牌:Car1

    Year: 2000年份:2000

  • Make: Car2品牌:Car2

    Year: 2017年份:2017

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM