首页 > 解决方案 > 从 XML 转换后使用 Json.Net 查询 JSON 时遇到问题

问题描述

我有一些 XML,我通过调用此方法将其转换为 JSON:

public static string SerializeAsJson(string xml)
{
    if (string.IsNullOrEmpty(xml)) throw new ArgumentNullException(nameof(xml));
    if (xml.StartsWith("{") && xml.EndsWith("}")) return xml;

    var doc = new XmlDocument();
    doc.LoadXml(xml);

    var json = JsonConvert.SerializeXmlNode(doc);
    return json;
}

这会返回:

{
"?xml":{
    "@version":"1.0",
    "@encoding":"UTF-8"
},
"product":{
    "productname":{
        "#cdata-section":"Artisan Cook Processor - Onyx Black, 4.5 litres, Keep-warm function, Dishwasher-safe pot, Timer"
    },
    "productshortname":{
        "#cdata-section":"Artisan Cook Processor - Onyx Black"
    },
    "productshortdesc":{
        "#cdata-section":"Rethink the way you cook with the KitchenAid Artisan Cook Processor in stylish onyx black. The ultimate helping hand Create delicious meals in no time with this all-in-one, multifunctional food processor. It really can do everything you need to cook up a feast - chop, crush, blend, mix, steam, steam and fry to name a few of the 25 helpful functions. Additionally, this KitchenAid worktop cooker benefits from a clever StirAssist asset, which gently flips and stirs your ingredients, so they still have the �stirred-by-hand', home-cooked effect when combined together. Simplicity, control and precision Create elegantly simple meals for one or test yourself with a whole dinner party of treats from scratch with the Artisan Cook Processor . The KitchenAid really makes life in the kitchen easier � use the hinged lid to open your processor whilst it is working, to add ingredients as you go, reducing mess and saving you time. Uniquely stylish The 4.5 litre KitchenAid Artisan comes in stainless steel with the added benefit of ergonomic, comfortable handles and a hinged lid for easy opening. It is reliable, robust and easy to clean with dishwasher-safe parts, meaning you don't need to spend hours manually maintaining it. Premium details such as metal knobs, levels and a die-cast metal body make this KitchenAid a real focal point of the kitchen, particularly with its striking onyx black colouring. Included extras Along with your KitchenAid Artisan Cook Processor , enjoy the step-by-step and easy to follow recipes for cooking delicious meals in your KitchenAid Artisan Processor . If you are looking for a streamlined appliance that has multiple functions covered, consider the KitchenAid Artisan Cook Processor."
    },
    "producthtmldesc":{
        "#cdata-section":"<p>Rethink the way you cook with the KitchenAid <strong>Artisan Cook Processor</strong> in stylish onyx black.<br /> <br /> <strong>The ultimate helping hand</strong><br /> <br /> Create delicious meals in no time with this all-in-one, multifunctional food processor. It really can do everything you need to cook up a feast - chop, crush, blend, mix, steam, steam and fry to name a few of the 25 helpful functions.<br /> <br /> Additionally, this KitchenAid worktop cooker benefits from a clever StirAssist asset, which gently flips and stirs your ingredients, so they still have the �stirred-by-hand', home-cooked effect when combined together.<br /> <br /> <strong>Simplicity, control and precision</strong><br /> <br /> Create elegantly simple meals for one or test yourself with a whole dinner party of treats from scratch with the <strong>Artisan Cook Processor</strong>. The KitchenAid really makes life in the kitchen easier � use the hinged lid to open your processor whilst it is working, to add ingredients as you go, reducing mess and saving you time.<br /> <br /> <strong>Uniquely stylish</strong><br /> <br /> The 4.5 litre KitchenAid <strong>Artisan</strong> comes in stainless steel with the added benefit of ergonomic, comfortable handles and a hinged lid for easy opening. It is reliable, robust and easy to clean with dishwasher-safe parts, meaning you dont need to spend hours manually maintaining it.<br /> <br /> Premium details such as metal knobs, levels and a die-cast metal body make this KitchenAid a real focal point of the kitchen, particularly with its striking onyx black colouring.<br /> <br /> <strong>Included extras</strong><br /> <br /> Along with your KitchenAid <strong>Artisan Cook Processor</strong>, enjoy the step-by-step and easy to follow recipes for cooking delicious meals in your KitchenAid <strong>Artisan Processor</strong>.<br /> <br /> If you are looking for a streamlined appliance that has multiple functions covered, consider the KitchenAid <strong>Artisan Cook Processor. </strong></p>"
    },
    "productlongdesc":{
        "#cdata-section":"Rethink the way you cook with the KitchenAid Artisan Cook Processor in stylish onyx black. The ultimate helping hand Create delicious meals in no time with this all-in-one, multifunctional food processor. It really can do everything you need to cook up a feast - chop, crush, blend, mix, steam, steam and fry to name a few of the 25 helpful functions. Additionally, this KitchenAid worktop cooker benefits from a clever StirAssist asset, which gently flips and stirs your ingredients, so they still have the �stirred-by-hand', home-cooked effect when combined together. Simplicity, control and precision Create elegantly simple meals for one or test yourself with a whole dinner party of treats from scratch with the Artisan Cook Processor . The KitchenAid really makes life in the kitchen easier � use the hinged lid to open your processor whilst it is working, to add ingredients as you go, reducing mess and saving you time. Uniquely stylish The 4.5 litre KitchenAid Artisan comes in stainless steel with the added benefit of ergonomic, comfortable handles and a hinged lid for easy opening. It is reliable, robust and easy to clean with dishwasher-safe parts, meaning you don't need to spend hours manually maintaining it. Premium details such as metal knobs, levels and a die-cast metal body make this KitchenAid a real focal point of the kitchen, particularly with its striking onyx black colouring. Included extras Along with your KitchenAid Artisan Cook Processor , enjoy the step-by-step and easy to follow recipes for cooking delicious meals in your KitchenAid Artisan Processor . If you are looking for a streamlined appliance that has multiple functions covered, consider the KitchenAid Artisan Cook Processor."
    },
    "productsecondaryname":{
        "#cdata-section":"4.5 litres, Keep-warm function, Dishwasher-safe pot, Timer"
    },
    "brand":{
        "#cdata-section":"KITCHENAID"
    },
    "infostech":null,
    "refexternal":{
        "#cdata-section":"144626"
    },
    "infotech":{
        "#cdata-section":"[{\\"         Title\\":\\"OVERVIEW\\",
        \\"Priority\\":1,
        \\"Attributes\\":[
            {
            \\"Title\\":\\"Type\\",
            \\"Value\\":\\"Multicooker\\",
            \\"Priority\\":0
            },
            {
            \\"Title\\":\\"Power\\",
            \\"Value\\":\\"1500 W\\",
            \\"Priority\\":2
            },
            {
            \\"Title\\":\\"Capacity\\",
            \\"Value\\":\\"4.5 litres\\",
            \\"Priority\\":1
            }
        ]
    },
    {
        \\"Title\\":\\"FUNCTIONS\\",
        \\"Priority\\":2,
        \\"Attributes\\":[
            {
            \\"Title\\":\\"Number of programmes\\",
            \\"Value\\":\\"25\\",
            \\"Priority\\":4
            },
            {
            \\"Title\\":\\"Timer\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":8
            },
            {
            \\"Title\\":\\"Number of heat settings\\",
            \\"Value\\":\\"Up to 140�C\\",
            \\"Priority\\":7
            },
            {
            \\"Title\\":\\"Keep warm function\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":6
            },
            {
            \\"Title\\":\\"Programs\\",
            \\"Value\\":\\"- Boil- Fry- Stew- Steam- Puree- Dough\\",
            \\"Priority\\":5
            }
        ]
    },
    {
        \\"Title\\":\\"FEATURES\\",
        \\"Priority\\":3,
        \\"Attributes\\":[
            {
            \\"Title\\":\\"Digital display\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":10
            },
            {
            \\"Title\\":\\"Automatic shut-off\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":18
            },
            {
            \\"Title\\":\\"Dishwasher safe parts\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":17
            },
            {
            \\"Title\\":\\"Microwave safe pot\\",
            \\"Value\\":\\"No\\",
            \\"Priority\\":16
            },
            {
            \\"Title\\":\\"Oven safe pot\\",
            \\"Value\\":\\"No\\",
            \\"Priority\\":15
            },
            {
            \\"Title\\":\\"Number of tiers\\",
            \\"Value\\":\\"3\\",
            \\"Priority\\":14
            },
            {
            \\"Title\\":\\"Sound signal\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":12
            },
            {
            \\"Title\\":\\"Power indicator\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":11
            }
        ]
    },
    {
        \\"Title\\":\\"GENERAL\\",
        \\"Priority\\":4,
        \\"Attributes\\":[
            {
            \\"Title\\":\\"Colour\\",
            \\"Value\\":\\"Black\\",
            \\"Priority\\":20
            },
            {
            \\"Title\\":\\"Manufacturer's guarantee\\",
            \\"Value\\":\\"3 years\\",
            \\"Priority\\":26
            },
            {
            \\"Title\\":\\"Weight\\",
            \\"Value\\":\\"10.14 kg\\",
            \\"Priority\\":25
            },
            {
            \\"Title\\":\\"Dimensions \\",
            \\"Value\\":\\"411 x 314 x 342 mm (H x W x D)\\",
            \\"Priority\\":24
            },
            {
            \\"Title\\":\\"Power cord length\\",
            \\"Value\\":\\"1 m\\",
            \\"Priority\\":23
            },
            {
            \\"Title\\":\\"Power cord storage\\",
            \\"Value\\":\\"No\\",
            \\"Priority\\":22
            },
            {
            \\"Title\\":\\"Box contents\\",
            \\"Value\\":\\"- StirAssist- Egg whip- Dough blade- Multiblade- Upper and lower steam baskets with lid- Inner basket- Mini bowl and mini multiblade- Measuring cup- Free cookbook\\",
            \\"Priority\\":21
            }
        ]
    }
]   "},"   id":{
    "#cdata-section":"10136069"
},
"images":{
    "urlimage1":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069.jpg",
    "urlimage2":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069.jpg",
    "urlimage3":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069.jpg",
    "urlimage4":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069.jpg",
    "urlimage5":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069.jpg",
    "urlimage6":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/u_10136069.jpg"
},
"images_alternate":{
    "urlimage1":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_003.jpg",
    "urlimage2":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_003.jpg",
    "urlimage3":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_003.jpg",
    "urlimage4":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_003.jpg",
    "urlimage5":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_003.jpg",
    "urlimage6":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_001.jpg",
    "urlimage7":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_001.jpg",
    "urlimage8":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_001.jpg",
    "urlimage9":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_001.jpg",
    "urlimage10":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_001.jpg",
    "urlimage11":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_006.jpg",
    "urlimage12":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_006.jpg",
    "urlimage13":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_006.jpg",
    "urlimage14":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_006.jpg",
    "urlimage15":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_006.jpg",
    "urlimage16":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_005.jpg",
    "urlimage17":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_005.jpg",
    "urlimage18":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_005.jpg",
    "urlimage19":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_005.jpg",
    "urlimage20":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_005.jpg",
    "urlimage21":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_002.jpg",
    "urlimage22":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_002.jpg",
    "urlimage23":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_002.jpg",
    "urlimage24":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_002.jpg",
    "urlimage25":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_002.jpg",
    "urlimage26":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_004.jpg",
    "urlimage27":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_004.jpg",
    "urlimage28":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_004.jpg",
    "urlimage29":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_004.jpg",
    "urlimage30":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_004.jpg"
},
"universe":{
    "#cdata-section":"Household Appliances"
},
"universeid":"35",
"category":{
    "#cdata-section":"Small kitchen appliances"
},
"categoryid":"336",
"market":{
    "#cdata-section":"Small cooking appliances"
},
"marketid":"3155",
"segment":{
    "#cdata-section":"Multicookers"
},
"segmentid":"31992",
"MPN":{
    "#cdata-section":"000000859792415020"
},
"EAN":"5413184100599",
"color":null,
"weight":"1"
}
}

我唯一感兴趣的部分是,infotech所以我尝试使用路径查询来获取该部分"product.infotech.#cdata-section",如下所示:

var json = JObject.Parse(response);
var products = json.SelectToken(path);

这给了我这个:

{
    [
        {
            "Title":"OVERVIEW",
            "Priority":1,
            "Attributes":[
                {
                "Title":"Type",
                "Value":"Multicooker",
                "Priority":0
                },
                {
                "Title":"Power",
                "Value":"1500 W",
                "Priority":2
                },
                {
                "Title":"Capacity",
                "Value":"4.5 litres",
                "Priority":1
                }
            ]
        },
        {
            "Title":"FUNCTIONS",
            "Priority":2,
            "Attributes":[
                {
                "Title":"Number of programmes",
                "Value":"25",
                "Priority":4
                },
                {
                "Title":"Timer",
                "Value":"Yes",
                "Priority":8
                },
                {
                "Title":"Number of heat settings",
                "Value":"Up to 140�C",
                "Priority":7
                },
                {
                "Title":"Keep warm function",
                "Value":"Yes",
                "Priority":6
                },
                {
                "Title":"Programs",
                "Value":"- Boil- Fry- Stew- Steam- Puree- Dough",
                "Priority":5
                }
            ]
        },
        {
            "Title":"FEATURES",
            "Priority":3,
            "Attributes":[
                {
                "Title":"Digital display",
                "Value":"Yes",
                "Priority":10
                },
                {
                "Title":"Automatic shut-off",
                "Value":"Yes",
                "Priority":18
                },
                {
                "Title":"Dishwasher safe parts",
                "Value":"Yes",
                "Priority":17
                },
                {
                "Title":"Microwave safe pot",
                "Value":"No",
                "Priority":16
                },
                {
                "Title":"Oven safe pot",
                "Value":"No",
                "Priority":15
                },
                {
                "Title":"Number of tiers",
                "Value":"3",
                "Priority":14
                },
                {
                "Title":"Sound signal",
                "Value":"Yes",
                "Priority":12
                },
                {
                "Title":"Power indicator",
                "Value":"Yes",
                "Priority":11
                }
            ]
        },
        {
            "Title":"GENERAL",
            "Priority":4,
            "Attributes":[
                {
                "Title":"Colour",
                "Value":"Black",
                "Priority":20
                },
                {
                "Title":"Manufacturer's guarantee",
                "Value":"3 years",
                "Priority":26
                },
                {
                "Title":"Weight",
                "Value":"10.14 kg",
                "Priority":25
                },
                {
                "Title":"Dimensions ",
                "Value":"411 x 314 x 342 mm (H x W x D)",
                "Priority":24
                },
                {
                "Title":"Power cord length",
                "Value":"1 m",
                "Priority":23
                },
                {
                "Title":"Power cord storage",
                "Value":"No",
                "Priority":22
                },
                {
                "Title":"Box contents",
                "Value":"- StirAssist- Egg whip- Dough blade- Multiblade- Upper and lower steam baskets with lid- Inner basket- Mini bowl and mini multiblade- Measuring cup- Free cookbook",
                "Priority":21
                }
            ]
        }
    ]
}

我想将其转换为IList<JObject>但无论我尝试什么,我都会得到一个错误。有人能帮我吗?

PS,如果我看一下产品的价值,它会显示一个数组:

在此处输入图像描述

标签: c#json.netjson-query

解决方案


我认为问题在于 的值product.infotech.#cdata-section是一个 JSON 字符串;它不是一个数组(还)。您可以在调试器屏幕截图中看到这一点:Type被列为String(not Array)。可能原始 XML 在该标记中有 JSON。当整个内容转换为 JSON 时,已经存在的 JSON 被转义,这就是所有额外的反斜杠都存在的原因。因此,要将值恢复为可用形式,您需要重新解析该部分:

var json = JObject.Parse(response);

// get the escaped JSON as a string
var productsJson = (string)json.SelectToken("product.infotech.#cdata-section");

// re-parse it to a JArray
var productsArray = JArray.Parse(productsJson);

// Now you can iterate over the array to access the JObjects within it
foreach (JObject item in productsArray)
{
    ...
}

推荐阅读